fault.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. *
  5. * Derived from MIPS:
  6. * Copyright (C) 1995 - 2000 by Ralf Baechle
  7. */
  8. #include <linux/context_tracking.h>
  9. #include <linux/signal.h>
  10. #include <linux/sched.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/entry-common.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp.h>
  22. #include <linux/kdebug.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/kfence.h>
  26. #include <asm/branch.h>
  27. #include <asm/exception.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/ptrace.h>
  30. int show_unhandled_signals = 1;
  31. static int __kprobes spurious_fault(unsigned long write, unsigned long address)
  32. {
  33. pgd_t *pgd;
  34. p4d_t *p4d;
  35. pud_t *pud;
  36. pmd_t *pmd;
  37. pte_t *pte;
  38. if (!(address & __UA_LIMIT))
  39. return 0;
  40. pgd = pgd_offset_k(address);
  41. if (!pgd_present(pgdp_get(pgd)))
  42. return 0;
  43. p4d = p4d_offset(pgd, address);
  44. if (!p4d_present(p4dp_get(p4d)))
  45. return 0;
  46. pud = pud_offset(p4d, address);
  47. if (!pud_present(pudp_get(pud)))
  48. return 0;
  49. pmd = pmd_offset(pud, address);
  50. if (!pmd_present(pmdp_get(pmd)))
  51. return 0;
  52. if (pmd_leaf(*pmd)) {
  53. return write ? pmd_write(pmdp_get(pmd)) : 1;
  54. } else {
  55. pte = pte_offset_kernel(pmd, address);
  56. if (!pte_present(ptep_get(pte)))
  57. return 0;
  58. return write ? pte_write(ptep_get(pte)) : 1;
  59. }
  60. }
  61. static void __kprobes no_context(struct pt_regs *regs,
  62. unsigned long write, unsigned long address)
  63. {
  64. const int field = sizeof(unsigned long) * 2;
  65. if (spurious_fault(write, address))
  66. return;
  67. /* Are we prepared to handle this kernel fault? */
  68. if (fixup_exception(regs))
  69. return;
  70. if (kfence_handle_page_fault(address, write, regs))
  71. return;
  72. /*
  73. * Oops. The kernel tried to access some bad page. We'll have to
  74. * terminate things with extreme prejudice.
  75. */
  76. bust_spinlocks(1);
  77. pr_alert("CPU %d Unable to handle kernel paging request at "
  78. "virtual address %0*lx, era == %0*lx, ra == %0*lx\n",
  79. raw_smp_processor_id(), field, address, field, regs->csr_era,
  80. field, regs->regs[1]);
  81. die("Oops", regs);
  82. }
  83. static void __kprobes do_out_of_memory(struct pt_regs *regs,
  84. unsigned long write, unsigned long address)
  85. {
  86. /*
  87. * We ran out of memory, call the OOM killer, and return the userspace
  88. * (which will retry the fault, or kill us if we got oom-killed).
  89. */
  90. if (!user_mode(regs)) {
  91. no_context(regs, write, address);
  92. return;
  93. }
  94. pagefault_out_of_memory();
  95. }
  96. static void __kprobes do_sigbus(struct pt_regs *regs,
  97. unsigned long write, unsigned long address, int si_code)
  98. {
  99. /* Kernel mode? Handle exceptions or die */
  100. if (!user_mode(regs)) {
  101. no_context(regs, write, address);
  102. return;
  103. }
  104. /*
  105. * Send a sigbus, regardless of whether we were in kernel
  106. * or user mode.
  107. */
  108. current->thread.csr_badvaddr = address;
  109. current->thread.trap_nr = read_csr_excode();
  110. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  111. }
  112. static void __kprobes do_sigsegv(struct pt_regs *regs,
  113. unsigned long write, unsigned long address, int si_code)
  114. {
  115. const int field = sizeof(unsigned long) * 2;
  116. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  117. /* Kernel mode? Handle exceptions or die */
  118. if (!user_mode(regs)) {
  119. no_context(regs, write, address);
  120. return;
  121. }
  122. /* User mode accesses just cause a SIGSEGV */
  123. current->thread.csr_badvaddr = address;
  124. if (!write)
  125. current->thread.error_code = 1;
  126. else
  127. current->thread.error_code = 2;
  128. current->thread.trap_nr = read_csr_excode();
  129. if (show_unhandled_signals &&
  130. unhandled_signal(current, SIGSEGV) && __ratelimit(&ratelimit_state)) {
  131. pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
  132. current->comm,
  133. write ? "write access to" : "read access from",
  134. field, address);
  135. pr_info("era = %0*lx in", field,
  136. (unsigned long) regs->csr_era);
  137. print_vma_addr(KERN_CONT " ", regs->csr_era);
  138. pr_cont("\n");
  139. pr_info("ra = %0*lx in", field,
  140. (unsigned long) regs->regs[1]);
  141. print_vma_addr(KERN_CONT " ", regs->regs[1]);
  142. pr_cont("\n");
  143. }
  144. force_sig_fault(SIGSEGV, si_code, (void __user *)address);
  145. }
  146. /*
  147. * This routine handles page faults. It determines the address,
  148. * and the problem, and then passes it off to one of the appropriate
  149. * routines.
  150. */
  151. static void __kprobes __do_page_fault(struct pt_regs *regs,
  152. unsigned long write, unsigned long address)
  153. {
  154. int si_code = SEGV_MAPERR;
  155. unsigned int flags = FAULT_FLAG_DEFAULT;
  156. struct task_struct *tsk = current;
  157. struct mm_struct *mm = tsk->mm;
  158. struct vm_area_struct *vma = NULL;
  159. vm_fault_t fault;
  160. if (kprobe_page_fault(regs, current->thread.trap_nr))
  161. return;
  162. /*
  163. * We fault-in kernel-space virtual memory on-demand. The
  164. * 'reference' page table is init_mm.pgd.
  165. *
  166. * NOTE! We MUST NOT take any locks for this case. We may
  167. * be in an interrupt or a critical region, and should
  168. * only copy the information from the master page table,
  169. * nothing more.
  170. */
  171. if (address & __UA_LIMIT) {
  172. if (!user_mode(regs))
  173. no_context(regs, write, address);
  174. else
  175. do_sigsegv(regs, write, address, si_code);
  176. return;
  177. }
  178. /*
  179. * If we're in an interrupt or have no user
  180. * context, we must not take the fault..
  181. */
  182. if (faulthandler_disabled() || !mm) {
  183. do_sigsegv(regs, write, address, si_code);
  184. return;
  185. }
  186. if (user_mode(regs))
  187. flags |= FAULT_FLAG_USER;
  188. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  189. retry:
  190. vma = lock_mm_and_find_vma(mm, address, regs);
  191. if (unlikely(!vma))
  192. goto bad_area_nosemaphore;
  193. goto good_area;
  194. /*
  195. * Something tried to access memory that isn't in our memory map..
  196. * Fix it, but check if it's kernel or user first..
  197. */
  198. bad_area:
  199. mmap_read_unlock(mm);
  200. bad_area_nosemaphore:
  201. do_sigsegv(regs, write, address, si_code);
  202. return;
  203. /*
  204. * Ok, we have a good vm_area for this memory access, so
  205. * we can handle it..
  206. */
  207. good_area:
  208. si_code = SEGV_ACCERR;
  209. if (write) {
  210. flags |= FAULT_FLAG_WRITE;
  211. if (!(vma->vm_flags & VM_WRITE))
  212. goto bad_area;
  213. } else {
  214. if (!(vma->vm_flags & VM_EXEC) && address == exception_era(regs))
  215. goto bad_area;
  216. if (!(vma->vm_flags & (VM_READ | VM_WRITE)) && address != exception_era(regs))
  217. goto bad_area;
  218. }
  219. /*
  220. * If for any reason at all we couldn't handle the fault,
  221. * make sure we exit gracefully rather than endlessly redo
  222. * the fault.
  223. */
  224. fault = handle_mm_fault(vma, address, flags, regs);
  225. if (fault_signal_pending(fault, regs)) {
  226. if (!user_mode(regs))
  227. no_context(regs, write, address);
  228. return;
  229. }
  230. /* The fault is fully completed (including releasing mmap lock) */
  231. if (fault & VM_FAULT_COMPLETED)
  232. return;
  233. if (unlikely(fault & VM_FAULT_RETRY)) {
  234. flags |= FAULT_FLAG_TRIED;
  235. /*
  236. * No need to mmap_read_unlock(mm) as we would
  237. * have already released it in __lock_page_or_retry
  238. * in mm/filemap.c.
  239. */
  240. goto retry;
  241. }
  242. if (unlikely(fault & VM_FAULT_ERROR)) {
  243. mmap_read_unlock(mm);
  244. if (fault & VM_FAULT_OOM) {
  245. do_out_of_memory(regs, write, address);
  246. return;
  247. } else if (fault & VM_FAULT_SIGSEGV) {
  248. do_sigsegv(regs, write, address, si_code);
  249. return;
  250. } else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  251. do_sigbus(regs, write, address, si_code);
  252. return;
  253. }
  254. BUG();
  255. }
  256. mmap_read_unlock(mm);
  257. }
  258. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  259. unsigned long write, unsigned long address)
  260. {
  261. irqentry_state_t state = irqentry_enter(regs);
  262. /* Enable interrupt if enabled in parent context */
  263. if (likely(regs->csr_prmd & CSR_PRMD_PIE))
  264. local_irq_enable();
  265. __do_page_fault(regs, write, address);
  266. local_irq_disable();
  267. irqentry_exit(regs, state);
  268. }