fault.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/signal.h>
  4. #include <linux/module.h>
  5. #include <linux/sched.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/mman.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/version.h>
  16. #include <linux/vt_kern.h>
  17. #include <linux/kernel.h>
  18. #include <linux/extable.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/hardirq.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/traps.h>
  23. #include <asm/page.h>
  24. int fixup_exception(struct pt_regs *regs)
  25. {
  26. const struct exception_table_entry *fixup;
  27. fixup = search_exception_tables(instruction_pointer(regs));
  28. if (fixup) {
  29. regs->pc = fixup->nextinsn;
  30. return 1;
  31. }
  32. return 0;
  33. }
  34. /*
  35. * This routine handles page faults. It determines the address,
  36. * and the problem, and then passes it off to one of the appropriate
  37. * routines.
  38. */
  39. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
  40. unsigned long mmu_meh)
  41. {
  42. struct vm_area_struct *vma = NULL;
  43. struct task_struct *tsk = current;
  44. struct mm_struct *mm = tsk->mm;
  45. int si_code;
  46. int fault;
  47. unsigned long address = mmu_meh & PAGE_MASK;
  48. si_code = SEGV_MAPERR;
  49. #ifndef CONFIG_CPU_HAS_TLBI
  50. /*
  51. * We fault-in kernel-space virtual memory on-demand. The
  52. * 'reference' page table is init_mm.pgd.
  53. *
  54. * NOTE! We MUST NOT take any locks for this case. We may
  55. * be in an interrupt or a critical region, and should
  56. * only copy the information from the master page table,
  57. * nothing more.
  58. */
  59. if (unlikely(address >= VMALLOC_START) &&
  60. unlikely(address <= VMALLOC_END)) {
  61. /*
  62. * Synchronize this task's top level page-table
  63. * with the 'reference' page table.
  64. *
  65. * Do _not_ use "tsk" here. We might be inside
  66. * an interrupt in the middle of a task switch..
  67. */
  68. int offset = __pgd_offset(address);
  69. pgd_t *pgd, *pgd_k;
  70. pud_t *pud, *pud_k;
  71. pmd_t *pmd, *pmd_k;
  72. pte_t *pte_k;
  73. unsigned long pgd_base;
  74. pgd_base = tlb_get_pgd();
  75. pgd = (pgd_t *)pgd_base + offset;
  76. pgd_k = init_mm.pgd + offset;
  77. if (!pgd_present(*pgd_k))
  78. goto no_context;
  79. set_pgd(pgd, *pgd_k);
  80. pud = (pud_t *)pgd;
  81. pud_k = (pud_t *)pgd_k;
  82. if (!pud_present(*pud_k))
  83. goto no_context;
  84. pmd = pmd_offset(pud, address);
  85. pmd_k = pmd_offset(pud_k, address);
  86. if (!pmd_present(*pmd_k))
  87. goto no_context;
  88. set_pmd(pmd, *pmd_k);
  89. pte_k = pte_offset_kernel(pmd_k, address);
  90. if (!pte_present(*pte_k))
  91. goto no_context;
  92. return;
  93. }
  94. #endif
  95. /*
  96. * If we're in an interrupt or have no user
  97. * context, we must not take the fault..
  98. */
  99. if (in_atomic() || !mm)
  100. goto bad_area_nosemaphore;
  101. down_read(&mm->mmap_sem);
  102. vma = find_vma(mm, address);
  103. if (!vma)
  104. goto bad_area;
  105. if (vma->vm_start <= address)
  106. goto good_area;
  107. if (!(vma->vm_flags & VM_GROWSDOWN))
  108. goto bad_area;
  109. if (expand_stack(vma, address))
  110. goto bad_area;
  111. /*
  112. * Ok, we have a good vm_area for this memory access, so
  113. * we can handle it..
  114. */
  115. good_area:
  116. si_code = SEGV_ACCERR;
  117. if (write) {
  118. if (!(vma->vm_flags & VM_WRITE))
  119. goto bad_area;
  120. } else {
  121. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  122. goto bad_area;
  123. }
  124. /*
  125. * If for any reason at all we couldn't handle the fault,
  126. * make sure we exit gracefully rather than endlessly redo
  127. * the fault.
  128. */
  129. fault = handle_mm_fault(vma, address, write ? FAULT_FLAG_WRITE : 0);
  130. if (unlikely(fault & VM_FAULT_ERROR)) {
  131. if (fault & VM_FAULT_OOM)
  132. goto out_of_memory;
  133. else if (fault & VM_FAULT_SIGBUS)
  134. goto do_sigbus;
  135. else if (fault & VM_FAULT_SIGSEGV)
  136. goto bad_area;
  137. BUG();
  138. }
  139. if (fault & VM_FAULT_MAJOR)
  140. tsk->maj_flt++;
  141. else
  142. tsk->min_flt++;
  143. up_read(&mm->mmap_sem);
  144. return;
  145. /*
  146. * Something tried to access memory that isn't in our memory map..
  147. * Fix it, but check if it's kernel or user first..
  148. */
  149. bad_area:
  150. up_read(&mm->mmap_sem);
  151. bad_area_nosemaphore:
  152. /* User mode accesses just cause a SIGSEGV */
  153. if (user_mode(regs)) {
  154. tsk->thread.address = address;
  155. tsk->thread.error_code = write;
  156. force_sig_fault(SIGSEGV, si_code, (void __user *)address, current);
  157. return;
  158. }
  159. no_context:
  160. /* Are we prepared to handle this kernel fault? */
  161. if (fixup_exception(regs))
  162. return;
  163. /*
  164. * Oops. The kernel tried to access some bad page. We'll have to
  165. * terminate things with extreme prejudice.
  166. */
  167. bust_spinlocks(1);
  168. pr_alert("Unable to %s at vaddr: %08lx, epc: %08lx\n",
  169. __func__, address, regs->pc);
  170. die_if_kernel("Oops", regs, write);
  171. out_of_memory:
  172. /*
  173. * We ran out of memory, call the OOM killer, and return the userspace
  174. * (which will retry the fault, or kill us if we got oom-killed).
  175. */
  176. pagefault_out_of_memory();
  177. return;
  178. do_sigbus:
  179. up_read(&mm->mmap_sem);
  180. /* Kernel mode? Handle exceptions or die */
  181. if (!user_mode(regs))
  182. goto no_context;
  183. tsk->thread.address = address;
  184. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, current);
  185. }