fault.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <linux/kfence.h>
  16. #include <linux/entry-common.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/tlbflush.h>
  19. #include "../kernel/head.h"
  20. static void die_kernel_fault(const char *msg, unsigned long addr,
  21. struct pt_regs *regs)
  22. {
  23. bust_spinlocks(1);
  24. pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", msg,
  25. addr);
  26. bust_spinlocks(0);
  27. die(regs, "Oops");
  28. make_task_dead(SIGKILL);
  29. }
  30. static inline void no_context(struct pt_regs *regs, unsigned long addr)
  31. {
  32. const char *msg;
  33. /* Are we prepared to handle this kernel fault? */
  34. if (fixup_exception(regs))
  35. return;
  36. /*
  37. * Oops. The kernel tried to access some bad page. We'll have to
  38. * terminate things with extreme prejudice.
  39. */
  40. if (addr < PAGE_SIZE)
  41. msg = "NULL pointer dereference";
  42. else {
  43. if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs))
  44. return;
  45. msg = "paging request";
  46. }
  47. die_kernel_fault(msg, addr, regs);
  48. }
  49. static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
  50. {
  51. if (!user_mode(regs)) {
  52. no_context(regs, addr);
  53. return;
  54. }
  55. if (fault & VM_FAULT_OOM) {
  56. /*
  57. * We ran out of memory, call the OOM killer, and return the userspace
  58. * (which will retry the fault, or kill us if we got oom-killed).
  59. */
  60. pagefault_out_of_memory();
  61. return;
  62. } else if (fault & (VM_FAULT_SIGBUS | VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) {
  63. /* Kernel mode? Handle exceptions or die */
  64. do_trap(regs, SIGBUS, BUS_ADRERR, addr);
  65. return;
  66. } else if (fault & VM_FAULT_SIGSEGV) {
  67. do_trap(regs, SIGSEGV, SEGV_MAPERR, addr);
  68. return;
  69. }
  70. BUG();
  71. }
  72. static inline void
  73. bad_area_nosemaphore(struct pt_regs *regs, int code, unsigned long addr)
  74. {
  75. /*
  76. * Something tried to access memory that isn't in our memory map.
  77. * Fix it, but check if it's kernel or user first.
  78. */
  79. /* User mode accesses just cause a SIGSEGV */
  80. if (user_mode(regs)) {
  81. do_trap(regs, SIGSEGV, code, addr);
  82. return;
  83. }
  84. no_context(regs, addr);
  85. }
  86. static inline void
  87. bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,
  88. unsigned long addr)
  89. {
  90. mmap_read_unlock(mm);
  91. bad_area_nosemaphore(regs, code, addr);
  92. }
  93. static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
  94. {
  95. pgd_t *pgd, *pgd_k;
  96. pud_t *pud_k;
  97. p4d_t *p4d_k;
  98. pmd_t *pmd_k;
  99. pte_t *pte_k;
  100. int index;
  101. unsigned long pfn;
  102. /* User mode accesses just cause a SIGSEGV */
  103. if (user_mode(regs))
  104. return do_trap(regs, SIGSEGV, code, addr);
  105. /*
  106. * Synchronize this task's top level page-table
  107. * with the 'reference' page table.
  108. *
  109. * Do _not_ use "tsk->active_mm->pgd" here.
  110. * We might be inside an interrupt in the middle
  111. * of a task switch.
  112. */
  113. index = pgd_index(addr);
  114. pfn = csr_read(CSR_SATP) & SATP_PPN;
  115. pgd = (pgd_t *)pfn_to_virt(pfn) + index;
  116. pgd_k = init_mm.pgd + index;
  117. if (!pgd_present(pgdp_get(pgd_k))) {
  118. no_context(regs, addr);
  119. return;
  120. }
  121. set_pgd(pgd, pgdp_get(pgd_k));
  122. p4d_k = p4d_offset(pgd_k, addr);
  123. if (!p4d_present(p4dp_get(p4d_k))) {
  124. no_context(regs, addr);
  125. return;
  126. }
  127. pud_k = pud_offset(p4d_k, addr);
  128. if (!pud_present(pudp_get(pud_k))) {
  129. no_context(regs, addr);
  130. return;
  131. }
  132. if (pud_leaf(pudp_get(pud_k)))
  133. goto flush_tlb;
  134. /*
  135. * Since the vmalloc area is global, it is unnecessary
  136. * to copy individual PTEs
  137. */
  138. pmd_k = pmd_offset(pud_k, addr);
  139. if (!pmd_present(pmdp_get(pmd_k))) {
  140. no_context(regs, addr);
  141. return;
  142. }
  143. if (pmd_leaf(pmdp_get(pmd_k)))
  144. goto flush_tlb;
  145. /*
  146. * Make sure the actual PTE exists as well to
  147. * catch kernel vmalloc-area accesses to non-mapped
  148. * addresses. If we don't do this, this will just
  149. * silently loop forever.
  150. */
  151. pte_k = pte_offset_kernel(pmd_k, addr);
  152. if (!pte_present(ptep_get(pte_k))) {
  153. no_context(regs, addr);
  154. return;
  155. }
  156. /*
  157. * The kernel assumes that TLBs don't cache invalid
  158. * entries, but in RISC-V, SFENCE.VMA specifies an
  159. * ordering constraint, not a cache flush; it is
  160. * necessary even after writing invalid entries.
  161. */
  162. flush_tlb:
  163. local_flush_tlb_page(addr);
  164. }
  165. static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
  166. {
  167. switch (cause) {
  168. case EXC_INST_PAGE_FAULT:
  169. if (!(vma->vm_flags & VM_EXEC)) {
  170. return true;
  171. }
  172. break;
  173. case EXC_LOAD_PAGE_FAULT:
  174. /* Write implies read */
  175. if (!(vma->vm_flags & (VM_READ | VM_WRITE))) {
  176. return true;
  177. }
  178. break;
  179. case EXC_STORE_PAGE_FAULT:
  180. if (!(vma->vm_flags & VM_WRITE)) {
  181. return true;
  182. }
  183. break;
  184. default:
  185. panic("%s: unhandled cause %lu", __func__, cause);
  186. }
  187. return false;
  188. }
  189. /*
  190. * This routine handles page faults. It determines the address and the
  191. * problem, and then passes it off to one of the appropriate routines.
  192. */
  193. void handle_page_fault(struct pt_regs *regs)
  194. {
  195. struct task_struct *tsk;
  196. struct vm_area_struct *vma;
  197. struct mm_struct *mm;
  198. unsigned long addr, cause;
  199. unsigned int flags = FAULT_FLAG_DEFAULT;
  200. int code = SEGV_MAPERR;
  201. vm_fault_t fault;
  202. cause = regs->cause;
  203. addr = regs->badaddr;
  204. tsk = current;
  205. mm = tsk->mm;
  206. if (kprobe_page_fault(regs, cause))
  207. return;
  208. /*
  209. * Fault-in kernel-space virtual memory on-demand.
  210. * The 'reference' page table is init_mm.pgd.
  211. *
  212. * NOTE! We MUST NOT take any locks for this case. We may
  213. * be in an interrupt or a critical region, and should
  214. * only copy the information from the master page table,
  215. * nothing more.
  216. */
  217. if ((!IS_ENABLED(CONFIG_MMU) || !IS_ENABLED(CONFIG_64BIT)) &&
  218. unlikely(addr >= VMALLOC_START && addr < VMALLOC_END)) {
  219. vmalloc_fault(regs, code, addr);
  220. return;
  221. }
  222. /* Enable interrupts if they were enabled in the parent context. */
  223. if (!regs_irqs_disabled(regs))
  224. local_irq_enable();
  225. /*
  226. * If we're in an interrupt, have no user context, or are running
  227. * in an atomic region, then we must not take the fault.
  228. */
  229. if (unlikely(faulthandler_disabled() || !mm)) {
  230. tsk->thread.bad_cause = cause;
  231. no_context(regs, addr);
  232. return;
  233. }
  234. if (user_mode(regs))
  235. flags |= FAULT_FLAG_USER;
  236. if (!user_mode(regs) && addr < TASK_SIZE && unlikely(!(regs->status & SR_SUM))) {
  237. if (fixup_exception(regs))
  238. return;
  239. die_kernel_fault("access to user memory without uaccess routines", addr, regs);
  240. }
  241. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  242. if (cause == EXC_STORE_PAGE_FAULT)
  243. flags |= FAULT_FLAG_WRITE;
  244. else if (cause == EXC_INST_PAGE_FAULT)
  245. flags |= FAULT_FLAG_INSTRUCTION;
  246. if (!(flags & FAULT_FLAG_USER))
  247. goto lock_mmap;
  248. vma = lock_vma_under_rcu(mm, addr);
  249. if (!vma)
  250. goto lock_mmap;
  251. if (unlikely(access_error(cause, vma))) {
  252. vma_end_read(vma);
  253. count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
  254. tsk->thread.bad_cause = cause;
  255. bad_area_nosemaphore(regs, SEGV_ACCERR, addr);
  256. return;
  257. }
  258. fault = handle_mm_fault(vma, addr, flags | FAULT_FLAG_VMA_LOCK, regs);
  259. if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
  260. vma_end_read(vma);
  261. if (!(fault & VM_FAULT_RETRY)) {
  262. count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
  263. goto done;
  264. }
  265. count_vm_vma_lock_event(VMA_LOCK_RETRY);
  266. if (fault & VM_FAULT_MAJOR)
  267. flags |= FAULT_FLAG_TRIED;
  268. if (fault_signal_pending(fault, regs)) {
  269. if (!user_mode(regs))
  270. no_context(regs, addr);
  271. return;
  272. }
  273. lock_mmap:
  274. retry:
  275. vma = lock_mm_and_find_vma(mm, addr, regs);
  276. if (unlikely(!vma)) {
  277. tsk->thread.bad_cause = cause;
  278. bad_area_nosemaphore(regs, code, addr);
  279. return;
  280. }
  281. /*
  282. * Ok, we have a good vm_area for this memory access, so
  283. * we can handle it.
  284. */
  285. code = SEGV_ACCERR;
  286. if (unlikely(access_error(cause, vma))) {
  287. tsk->thread.bad_cause = cause;
  288. bad_area(regs, mm, code, addr);
  289. return;
  290. }
  291. /*
  292. * If for any reason at all we could not handle the fault,
  293. * make sure we exit gracefully rather than endlessly redo
  294. * the fault.
  295. */
  296. fault = handle_mm_fault(vma, addr, flags, regs);
  297. /*
  298. * If we need to retry but a fatal signal is pending, handle the
  299. * signal first. We do not need to release the mmap_lock because it
  300. * would already be released in __lock_page_or_retry in mm/filemap.c.
  301. */
  302. if (fault_signal_pending(fault, regs)) {
  303. if (!user_mode(regs))
  304. no_context(regs, addr);
  305. return;
  306. }
  307. /* The fault is fully completed (including releasing mmap lock) */
  308. if (fault & VM_FAULT_COMPLETED)
  309. return;
  310. if (unlikely(fault & VM_FAULT_RETRY)) {
  311. flags |= FAULT_FLAG_TRIED;
  312. /*
  313. * No need to mmap_read_unlock(mm) as we would
  314. * have already released it in __lock_page_or_retry
  315. * in mm/filemap.c.
  316. */
  317. goto retry;
  318. }
  319. mmap_read_unlock(mm);
  320. done:
  321. if (unlikely(fault & VM_FAULT_ERROR)) {
  322. tsk->thread.bad_cause = cause;
  323. mm_fault_error(regs, addr, fault);
  324. return;
  325. }
  326. return;
  327. }