fault.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/extable.h>
  4. #include <linux/kprobes.h>
  5. #include <linux/mmu_context.h>
  6. #include <linux/perf_event.h>
  7. int fixup_exception(struct pt_regs *regs)
  8. {
  9. const struct exception_table_entry *fixup;
  10. fixup = search_exception_tables(instruction_pointer(regs));
  11. if (fixup) {
  12. regs->pc = fixup->fixup;
  13. return 1;
  14. }
  15. return 0;
  16. }
  17. static inline bool is_write(struct pt_regs *regs)
  18. {
  19. switch (trap_no(regs)) {
  20. case VEC_TLBINVALIDS:
  21. return true;
  22. case VEC_TLBMODIFIED:
  23. return true;
  24. }
  25. return false;
  26. }
  27. #ifdef CONFIG_CPU_HAS_LDSTEX
  28. static inline void csky_cmpxchg_fixup(struct pt_regs *regs)
  29. {
  30. return;
  31. }
  32. #else
  33. extern unsigned long csky_cmpxchg_ldw;
  34. extern unsigned long csky_cmpxchg_stw;
  35. static inline void csky_cmpxchg_fixup(struct pt_regs *regs)
  36. {
  37. if (trap_no(regs) != VEC_TLBMODIFIED)
  38. return;
  39. if (instruction_pointer(regs) == csky_cmpxchg_stw)
  40. instruction_pointer_set(regs, csky_cmpxchg_ldw);
  41. return;
  42. }
  43. #endif
  44. static inline void no_context(struct pt_regs *regs, unsigned long addr)
  45. {
  46. current->thread.trap_no = trap_no(regs);
  47. /* Are we prepared to handle this kernel fault? */
  48. if (fixup_exception(regs))
  49. return;
  50. /*
  51. * Oops. The kernel tried to access some bad page. We'll have to
  52. * terminate things with extreme prejudice.
  53. */
  54. bust_spinlocks(1);
  55. pr_alert("Unable to handle kernel paging request at virtual "
  56. "addr 0x%08lx, pc: 0x%08lx\n", addr, regs->pc);
  57. die(regs, "Oops");
  58. make_task_dead(SIGKILL);
  59. }
  60. static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
  61. {
  62. current->thread.trap_no = trap_no(regs);
  63. if (fault & VM_FAULT_OOM) {
  64. /*
  65. * We ran out of memory, call the OOM killer, and return the userspace
  66. * (which will retry the fault, or kill us if we got oom-killed).
  67. */
  68. if (!user_mode(regs)) {
  69. no_context(regs, addr);
  70. return;
  71. }
  72. pagefault_out_of_memory();
  73. return;
  74. } else if (fault & VM_FAULT_SIGBUS) {
  75. /* Kernel mode? Handle exceptions or die */
  76. if (!user_mode(regs)) {
  77. no_context(regs, addr);
  78. return;
  79. }
  80. do_trap(regs, SIGBUS, BUS_ADRERR, addr);
  81. return;
  82. }
  83. BUG();
  84. }
  85. static inline void bad_area_nosemaphore(struct pt_regs *regs, struct mm_struct *mm, int code, unsigned long addr)
  86. {
  87. /*
  88. * Something tried to access memory that isn't in our memory map.
  89. * Fix it, but check if it's kernel or user first.
  90. */
  91. /* User mode accesses just cause a SIGSEGV */
  92. if (user_mode(regs)) {
  93. do_trap(regs, SIGSEGV, code, addr);
  94. return;
  95. }
  96. no_context(regs, addr);
  97. }
  98. static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
  99. {
  100. pgd_t *pgd, *pgd_k;
  101. pud_t *pud, *pud_k;
  102. pmd_t *pmd, *pmd_k;
  103. pte_t *pte_k;
  104. int offset;
  105. /* User mode accesses just cause a SIGSEGV */
  106. if (user_mode(regs)) {
  107. do_trap(regs, SIGSEGV, code, addr);
  108. return;
  109. }
  110. /*
  111. * Synchronize this task's top level page-table
  112. * with the 'reference' page table.
  113. *
  114. * Do _not_ use "tsk" here. We might be inside
  115. * an interrupt in the middle of a task switch..
  116. */
  117. offset = pgd_index(addr);
  118. pgd = get_pgd() + offset;
  119. pgd_k = init_mm.pgd + offset;
  120. if (!pgd_present(*pgd_k)) {
  121. no_context(regs, addr);
  122. return;
  123. }
  124. set_pgd(pgd, *pgd_k);
  125. pud = (pud_t *)pgd;
  126. pud_k = (pud_t *)pgd_k;
  127. if (!pud_present(*pud_k)) {
  128. no_context(regs, addr);
  129. return;
  130. }
  131. pmd = pmd_offset(pud, addr);
  132. pmd_k = pmd_offset(pud_k, addr);
  133. if (!pmd_present(*pmd_k)) {
  134. no_context(regs, addr);
  135. return;
  136. }
  137. set_pmd(pmd, *pmd_k);
  138. pte_k = pte_offset_kernel(pmd_k, addr);
  139. if (!pte_present(*pte_k)) {
  140. no_context(regs, addr);
  141. return;
  142. }
  143. flush_tlb_one(addr);
  144. }
  145. static inline bool access_error(struct pt_regs *regs, struct vm_area_struct *vma)
  146. {
  147. if (is_write(regs)) {
  148. if (!(vma->vm_flags & VM_WRITE))
  149. return true;
  150. } else {
  151. if (unlikely(!vma_is_accessible(vma)))
  152. return true;
  153. }
  154. return false;
  155. }
  156. /*
  157. * This routine handles page faults. It determines the address and the
  158. * problem, and then passes it off to one of the appropriate routines.
  159. */
  160. asmlinkage void do_page_fault(struct pt_regs *regs)
  161. {
  162. struct task_struct *tsk;
  163. struct vm_area_struct *vma;
  164. struct mm_struct *mm;
  165. unsigned long addr = read_mmu_entryhi() & PAGE_MASK;
  166. unsigned int flags = FAULT_FLAG_DEFAULT;
  167. int code = SEGV_MAPERR;
  168. vm_fault_t fault;
  169. tsk = current;
  170. mm = tsk->mm;
  171. csky_cmpxchg_fixup(regs);
  172. if (kprobe_page_fault(regs, tsk->thread.trap_no))
  173. return;
  174. /*
  175. * Fault-in kernel-space virtual memory on-demand.
  176. * The 'reference' page table is init_mm.pgd.
  177. *
  178. * NOTE! We MUST NOT take any locks for this case. We may
  179. * be in an interrupt or a critical region, and should
  180. * only copy the information from the master page table,
  181. * nothing more.
  182. */
  183. if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END))) {
  184. vmalloc_fault(regs, code, addr);
  185. return;
  186. }
  187. /* Enable interrupts if they were enabled in the parent context. */
  188. if (likely(regs->sr & BIT(6)))
  189. local_irq_enable();
  190. /*
  191. * If we're in an interrupt, have no user context, or are running
  192. * in an atomic region, then we must not take the fault.
  193. */
  194. if (unlikely(faulthandler_disabled() || !mm)) {
  195. no_context(regs, addr);
  196. return;
  197. }
  198. if (user_mode(regs))
  199. flags |= FAULT_FLAG_USER;
  200. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  201. if (is_write(regs))
  202. flags |= FAULT_FLAG_WRITE;
  203. retry:
  204. vma = lock_mm_and_find_vma(mm, address, regs);
  205. if (unlikely(!vma)) {
  206. bad_area_nosemaphore(regs, mm, code, addr);
  207. return;
  208. }
  209. /*
  210. * Ok, we have a good vm_area for this memory access, so
  211. * we can handle it.
  212. */
  213. code = SEGV_ACCERR;
  214. if (unlikely(access_error(regs, vma))) {
  215. mmap_read_unlock(mm);
  216. bad_area_nosemaphore(regs, mm, code, addr);
  217. return;
  218. }
  219. /*
  220. * If for any reason at all we could not handle the fault,
  221. * make sure we exit gracefully rather than endlessly redo
  222. * the fault.
  223. */
  224. fault = handle_mm_fault(vma, addr, flags, regs);
  225. /*
  226. * If we need to retry but a fatal signal is pending, handle the
  227. * signal first. We do not need to release the mmap_lock because it
  228. * would already be released in __lock_page_or_retry in mm/filemap.c.
  229. */
  230. if (fault_signal_pending(fault, regs)) {
  231. if (!user_mode(regs))
  232. no_context(regs, addr);
  233. return;
  234. }
  235. /* The fault is fully completed (including releasing mmap lock) */
  236. if (fault & VM_FAULT_COMPLETED)
  237. return;
  238. if (unlikely((fault & VM_FAULT_RETRY) && (flags & FAULT_FLAG_ALLOW_RETRY))) {
  239. flags |= FAULT_FLAG_TRIED;
  240. /*
  241. * No need to mmap_read_unlock(mm) as we would
  242. * have already released it in __lock_page_or_retry
  243. * in mm/filemap.c.
  244. */
  245. goto retry;
  246. }
  247. mmap_read_unlock(mm);
  248. if (unlikely(fault & VM_FAULT_ERROR)) {
  249. mm_fault_error(regs, addr, fault);
  250. return;
  251. }
  252. return;
  253. }