vm_fault.c 3.7 KB

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