vm_fault.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Memory fault handling for Hexagon
  4. *
  5. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  6. */
  7. /*
  8. * Page fault handling for the Hexagon Virtual Machine.
  9. * Can also be called by a native port emulating the HVM
  10. * execptions.
  11. */
  12. #include <asm/traps.h>
  13. #include <asm/vm_fault.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/mm.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/signal.h>
  18. #include <linux/extable.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/perf_event.h>
  21. /*
  22. * Decode of hardware exception sends us to one of several
  23. * entry points. At each, we generate canonical arguments
  24. * for handling by the abstract memory management code.
  25. */
  26. #define FLT_IFETCH -1
  27. #define FLT_LOAD 0
  28. #define FLT_STORE 1
  29. /*
  30. * Canonical page fault handler
  31. */
  32. static void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
  33. {
  34. struct vm_area_struct *vma;
  35. struct mm_struct *mm = current->mm;
  36. int si_signo;
  37. int si_code = SEGV_MAPERR;
  38. vm_fault_t fault;
  39. const struct exception_table_entry *fixup;
  40. unsigned int flags = FAULT_FLAG_DEFAULT;
  41. /*
  42. * If we're in an interrupt or have no user context,
  43. * then must not take the fault.
  44. */
  45. if (unlikely(in_interrupt() || !mm))
  46. goto no_context;
  47. local_irq_enable();
  48. if (user_mode(regs))
  49. flags |= FAULT_FLAG_USER;
  50. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  51. retry:
  52. vma = lock_mm_and_find_vma(mm, address, regs);
  53. if (unlikely(!vma))
  54. goto bad_area_nosemaphore;
  55. /* Address space is OK. Now check access rights. */
  56. si_code = SEGV_ACCERR;
  57. switch (cause) {
  58. case FLT_IFETCH:
  59. if (!(vma->vm_flags & VM_EXEC))
  60. goto bad_area;
  61. break;
  62. case FLT_LOAD:
  63. if (!(vma->vm_flags & VM_READ))
  64. goto bad_area;
  65. break;
  66. case FLT_STORE:
  67. if (!(vma->vm_flags & VM_WRITE))
  68. goto bad_area;
  69. flags |= FAULT_FLAG_WRITE;
  70. break;
  71. }
  72. fault = handle_mm_fault(vma, address, flags, regs);
  73. if (fault_signal_pending(fault, regs)) {
  74. if (!user_mode(regs))
  75. goto no_context;
  76. return;
  77. }
  78. /* The fault is fully completed (including releasing mmap lock) */
  79. if (fault & VM_FAULT_COMPLETED)
  80. return;
  81. /* The most common case -- we are done. */
  82. if (likely(!(fault & VM_FAULT_ERROR))) {
  83. if (fault & VM_FAULT_RETRY) {
  84. flags |= FAULT_FLAG_TRIED;
  85. goto retry;
  86. }
  87. mmap_read_unlock(mm);
  88. return;
  89. }
  90. mmap_read_unlock(mm);
  91. /* Handle copyin/out exception cases */
  92. if (!user_mode(regs))
  93. goto no_context;
  94. if (fault & VM_FAULT_OOM) {
  95. pagefault_out_of_memory();
  96. return;
  97. }
  98. /* User-mode address is in the memory map, but we are
  99. * unable to fix up the page fault.
  100. */
  101. if (fault & VM_FAULT_SIGBUS) {
  102. si_signo = SIGBUS;
  103. si_code = BUS_ADRERR;
  104. }
  105. /* Address is not in the memory map */
  106. else {
  107. si_signo = SIGSEGV;
  108. si_code = SEGV_ACCERR;
  109. }
  110. force_sig_fault(si_signo, si_code, (void __user *)address);
  111. return;
  112. bad_area:
  113. mmap_read_unlock(mm);
  114. bad_area_nosemaphore:
  115. if (user_mode(regs)) {
  116. force_sig_fault(SIGSEGV, si_code, (void __user *)address);
  117. return;
  118. }
  119. /* Kernel-mode fault falls through */
  120. no_context:
  121. fixup = search_exception_tables(pt_elr(regs));
  122. if (fixup) {
  123. pt_set_elr(regs, fixup->fixup);
  124. return;
  125. }
  126. /* Things are looking very, very bad now */
  127. bust_spinlocks(1);
  128. printk(KERN_EMERG "Unable to handle kernel paging request at "
  129. "virtual address 0x%08lx, regs %p\n", address, regs);
  130. die("Bad Kernel VA", regs, SIGKILL);
  131. }
  132. void read_protection_fault(struct pt_regs *regs)
  133. {
  134. unsigned long badvadr = pt_badva(regs);
  135. do_page_fault(badvadr, FLT_LOAD, regs);
  136. }
  137. void write_protection_fault(struct pt_regs *regs)
  138. {
  139. unsigned long badvadr = pt_badva(regs);
  140. do_page_fault(badvadr, FLT_STORE, regs);
  141. }
  142. void execute_protection_fault(struct pt_regs *regs)
  143. {
  144. unsigned long badvadr = pt_badva(regs);
  145. do_page_fault(badvadr, FLT_IFETCH, regs);
  146. }