fault.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/kprobes.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/branch.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/ptrace.h>
  29. int show_unhandled_signals = 1;
  30. static void __kprobes no_context(struct pt_regs *regs, unsigned long address)
  31. {
  32. const int field = sizeof(unsigned long) * 2;
  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. bust_spinlocks(1);
  41. pr_alert("CPU %d Unable to handle kernel paging request at "
  42. "virtual address %0*lx, era == %0*lx, ra == %0*lx\n",
  43. raw_smp_processor_id(), field, address, field, regs->csr_era,
  44. field, regs->regs[1]);
  45. die("Oops", regs);
  46. }
  47. static void __kprobes do_out_of_memory(struct pt_regs *regs, unsigned long address)
  48. {
  49. /*
  50. * We ran out of memory, call the OOM killer, and return the userspace
  51. * (which will retry the fault, or kill us if we got oom-killed).
  52. */
  53. if (!user_mode(regs)) {
  54. no_context(regs, address);
  55. return;
  56. }
  57. pagefault_out_of_memory();
  58. }
  59. static void __kprobes do_sigbus(struct pt_regs *regs,
  60. unsigned long write, unsigned long address, int si_code)
  61. {
  62. /* Kernel mode? Handle exceptions or die */
  63. if (!user_mode(regs)) {
  64. no_context(regs, address);
  65. return;
  66. }
  67. /*
  68. * Send a sigbus, regardless of whether we were in kernel
  69. * or user mode.
  70. */
  71. current->thread.csr_badvaddr = address;
  72. current->thread.trap_nr = read_csr_excode();
  73. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  74. }
  75. static void __kprobes do_sigsegv(struct pt_regs *regs,
  76. unsigned long write, unsigned long address, int si_code)
  77. {
  78. const int field = sizeof(unsigned long) * 2;
  79. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  80. /* Kernel mode? Handle exceptions or die */
  81. if (!user_mode(regs)) {
  82. no_context(regs, address);
  83. return;
  84. }
  85. /* User mode accesses just cause a SIGSEGV */
  86. current->thread.csr_badvaddr = address;
  87. if (!write)
  88. current->thread.error_code = 1;
  89. else
  90. current->thread.error_code = 2;
  91. current->thread.trap_nr = read_csr_excode();
  92. if (show_unhandled_signals &&
  93. unhandled_signal(current, SIGSEGV) && __ratelimit(&ratelimit_state)) {
  94. pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
  95. current->comm,
  96. write ? "write access to" : "read access from",
  97. field, address);
  98. pr_info("era = %0*lx in", field,
  99. (unsigned long) regs->csr_era);
  100. print_vma_addr(KERN_CONT " ", regs->csr_era);
  101. pr_cont("\n");
  102. pr_info("ra = %0*lx in", field,
  103. (unsigned long) regs->regs[1]);
  104. print_vma_addr(KERN_CONT " ", regs->regs[1]);
  105. pr_cont("\n");
  106. }
  107. force_sig_fault(SIGSEGV, si_code, (void __user *)address);
  108. }
  109. /*
  110. * This routine handles page faults. It determines the address,
  111. * and the problem, and then passes it off to one of the appropriate
  112. * routines.
  113. */
  114. static void __kprobes __do_page_fault(struct pt_regs *regs,
  115. unsigned long write, unsigned long address)
  116. {
  117. int si_code = SEGV_MAPERR;
  118. unsigned int flags = FAULT_FLAG_DEFAULT;
  119. struct task_struct *tsk = current;
  120. struct mm_struct *mm = tsk->mm;
  121. struct vm_area_struct *vma = NULL;
  122. vm_fault_t fault;
  123. /*
  124. * We fault-in kernel-space virtual memory on-demand. The
  125. * 'reference' page table is init_mm.pgd.
  126. *
  127. * NOTE! We MUST NOT take any locks for this case. We may
  128. * be in an interrupt or a critical region, and should
  129. * only copy the information from the master page table,
  130. * nothing more.
  131. */
  132. if (address & __UA_LIMIT) {
  133. if (!user_mode(regs))
  134. no_context(regs, address);
  135. else
  136. do_sigsegv(regs, write, address, si_code);
  137. return;
  138. }
  139. /*
  140. * If we're in an interrupt or have no user
  141. * context, we must not take the fault..
  142. */
  143. if (faulthandler_disabled() || !mm) {
  144. do_sigsegv(regs, write, address, si_code);
  145. return;
  146. }
  147. if (user_mode(regs))
  148. flags |= FAULT_FLAG_USER;
  149. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  150. retry:
  151. vma = lock_mm_and_find_vma(mm, address, regs);
  152. if (unlikely(!vma))
  153. goto bad_area_nosemaphore;
  154. goto good_area;
  155. /*
  156. * Something tried to access memory that isn't in our memory map..
  157. * Fix it, but check if it's kernel or user first..
  158. */
  159. bad_area:
  160. mmap_read_unlock(mm);
  161. bad_area_nosemaphore:
  162. do_sigsegv(regs, write, address, si_code);
  163. return;
  164. /*
  165. * Ok, we have a good vm_area for this memory access, so
  166. * we can handle it..
  167. */
  168. good_area:
  169. si_code = SEGV_ACCERR;
  170. if (write) {
  171. flags |= FAULT_FLAG_WRITE;
  172. if (!(vma->vm_flags & VM_WRITE))
  173. goto bad_area;
  174. } else {
  175. if (!(vma->vm_flags & VM_READ) && address != exception_era(regs))
  176. goto bad_area;
  177. if (!(vma->vm_flags & VM_EXEC) && address == exception_era(regs))
  178. goto bad_area;
  179. }
  180. /*
  181. * If for any reason at all we couldn't handle the fault,
  182. * make sure we exit gracefully rather than endlessly redo
  183. * the fault.
  184. */
  185. fault = handle_mm_fault(vma, address, flags, regs);
  186. if (fault_signal_pending(fault, regs)) {
  187. if (!user_mode(regs))
  188. no_context(regs, address);
  189. return;
  190. }
  191. /* The fault is fully completed (including releasing mmap lock) */
  192. if (fault & VM_FAULT_COMPLETED)
  193. return;
  194. if (unlikely(fault & VM_FAULT_RETRY)) {
  195. flags |= FAULT_FLAG_TRIED;
  196. /*
  197. * No need to mmap_read_unlock(mm) as we would
  198. * have already released it in __lock_page_or_retry
  199. * in mm/filemap.c.
  200. */
  201. goto retry;
  202. }
  203. if (unlikely(fault & VM_FAULT_ERROR)) {
  204. mmap_read_unlock(mm);
  205. if (fault & VM_FAULT_OOM) {
  206. do_out_of_memory(regs, address);
  207. return;
  208. } else if (fault & VM_FAULT_SIGSEGV) {
  209. do_sigsegv(regs, write, address, si_code);
  210. return;
  211. } else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  212. do_sigbus(regs, write, address, si_code);
  213. return;
  214. }
  215. BUG();
  216. }
  217. mmap_read_unlock(mm);
  218. }
  219. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  220. unsigned long write, unsigned long address)
  221. {
  222. irqentry_state_t state = irqentry_enter(regs);
  223. /* Enable interrupt if enabled in parent context */
  224. if (likely(regs->csr_prmd & CSR_PRMD_PIE))
  225. local_irq_enable();
  226. __do_page_fault(regs, write, address);
  227. local_irq_disable();
  228. irqentry_exit(regs, state);
  229. }