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