fault.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Page Fault Handling for ARC (TLB Miss / ProtV)
  3. *
  4. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  5. */
  6. #include <linux/signal.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/errno.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/mm_types.h>
  15. #include <asm/entry.h>
  16. #include <asm/mmu.h>
  17. /*
  18. * kernel virtual address is required to implement vmalloc/pkmap/fixmap
  19. * Refer to asm/processor.h for System Memory Map
  20. *
  21. * It simply copies the PMD entry (pointer to 2nd level page table or hugepage)
  22. * from swapper pgdir to task pgdir. The 2nd level table/page is thus shared
  23. */
  24. noinline static int handle_kernel_vaddr_fault(unsigned long address)
  25. {
  26. /*
  27. * Synchronize this task's top level page-table
  28. * with the 'reference' page table.
  29. */
  30. pgd_t *pgd, *pgd_k;
  31. p4d_t *p4d, *p4d_k;
  32. pud_t *pud, *pud_k;
  33. pmd_t *pmd, *pmd_k;
  34. pgd = pgd_offset(current->active_mm, address);
  35. pgd_k = pgd_offset_k(address);
  36. if (pgd_none (*pgd_k))
  37. goto bad_area;
  38. if (!pgd_present(*pgd))
  39. set_pgd(pgd, *pgd_k);
  40. p4d = p4d_offset(pgd, address);
  41. p4d_k = p4d_offset(pgd_k, address);
  42. if (p4d_none(*p4d_k))
  43. goto bad_area;
  44. if (!p4d_present(*p4d))
  45. set_p4d(p4d, *p4d_k);
  46. pud = pud_offset(p4d, address);
  47. pud_k = pud_offset(p4d_k, address);
  48. if (pud_none(*pud_k))
  49. goto bad_area;
  50. if (!pud_present(*pud))
  51. set_pud(pud, *pud_k);
  52. pmd = pmd_offset(pud, address);
  53. pmd_k = pmd_offset(pud_k, address);
  54. if (pmd_none(*pmd_k))
  55. goto bad_area;
  56. if (!pmd_present(*pmd))
  57. set_pmd(pmd, *pmd_k);
  58. /* XXX: create the TLB entry here */
  59. return 0;
  60. bad_area:
  61. return 1;
  62. }
  63. void do_page_fault(unsigned long address, struct pt_regs *regs)
  64. {
  65. struct vm_area_struct *vma = NULL;
  66. struct task_struct *tsk = current;
  67. struct mm_struct *mm = tsk->mm;
  68. int sig, si_code = SEGV_MAPERR;
  69. unsigned int write = 0, exec = 0, mask;
  70. vm_fault_t fault = VM_FAULT_SIGSEGV; /* handle_mm_fault() output */
  71. unsigned int flags; /* handle_mm_fault() input */
  72. /*
  73. * NOTE! We MUST NOT take any locks for this case. We may
  74. * be in an interrupt or a critical region, and should
  75. * only copy the information from the master page table,
  76. * nothing more.
  77. */
  78. if (address >= VMALLOC_START && !user_mode(regs)) {
  79. if (unlikely(handle_kernel_vaddr_fault(address)))
  80. goto no_context;
  81. else
  82. return;
  83. }
  84. /*
  85. * If we're in an interrupt or have no user
  86. * context, we must not take the fault..
  87. */
  88. if (faulthandler_disabled() || !mm)
  89. goto no_context;
  90. if (regs->ecr.cause & ECR_C_PROTV_STORE) /* ST/EX */
  91. write = 1;
  92. else if ((regs->ecr.vec == ECR_V_PROTV) &&
  93. (regs->ecr.cause == ECR_C_PROTV_INST_FETCH))
  94. exec = 1;
  95. flags = FAULT_FLAG_DEFAULT;
  96. if (user_mode(regs))
  97. flags |= FAULT_FLAG_USER;
  98. if (write)
  99. flags |= FAULT_FLAG_WRITE;
  100. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  101. retry:
  102. vma = lock_mm_and_find_vma(mm, address, regs);
  103. if (!vma)
  104. goto bad_area_nosemaphore;
  105. /*
  106. * vm_area is good, now check permissions for this memory access
  107. */
  108. mask = VM_READ;
  109. if (write)
  110. mask = VM_WRITE;
  111. if (exec)
  112. mask = VM_EXEC;
  113. if (!(vma->vm_flags & mask)) {
  114. si_code = SEGV_ACCERR;
  115. goto bad_area;
  116. }
  117. fault = handle_mm_fault(vma, address, flags, regs);
  118. /* Quick path to respond to signals */
  119. if (fault_signal_pending(fault, regs)) {
  120. if (!user_mode(regs))
  121. goto no_context;
  122. return;
  123. }
  124. /* The fault is fully completed (including releasing mmap lock) */
  125. if (fault & VM_FAULT_COMPLETED)
  126. return;
  127. /*
  128. * Fault retry nuances, mmap_lock already relinquished by core mm
  129. */
  130. if (unlikely(fault & VM_FAULT_RETRY)) {
  131. flags |= FAULT_FLAG_TRIED;
  132. goto retry;
  133. }
  134. bad_area:
  135. mmap_read_unlock(mm);
  136. bad_area_nosemaphore:
  137. /*
  138. * Major/minor page fault accounting
  139. * (in case of retry we only land here once)
  140. */
  141. if (likely(!(fault & VM_FAULT_ERROR)))
  142. /* Normal return path: fault Handled Gracefully */
  143. return;
  144. if (!user_mode(regs))
  145. goto no_context;
  146. if (fault & VM_FAULT_OOM) {
  147. pagefault_out_of_memory();
  148. return;
  149. }
  150. if (fault & VM_FAULT_SIGBUS) {
  151. sig = SIGBUS;
  152. si_code = BUS_ADRERR;
  153. }
  154. else {
  155. sig = SIGSEGV;
  156. }
  157. tsk->thread.fault_address = address;
  158. force_sig_fault(sig, si_code, (void __user *)address);
  159. return;
  160. no_context:
  161. if (fixup_exception(regs))
  162. return;
  163. die("Oops", regs, address);
  164. }