vm_fault.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Memory fault handling for Hexagon
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. /*
  21. * Page fault handling for the Hexagon Virtual Machine.
  22. * Can also be called by a native port emulating the HVM
  23. * execptions.
  24. */
  25. #include <asm/pgtable.h>
  26. #include <asm/traps.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/mm.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/signal.h>
  31. #include <linux/extable.h>
  32. #include <linux/hardirq.h>
  33. /*
  34. * Decode of hardware exception sends us to one of several
  35. * entry points. At each, we generate canonical arguments
  36. * for handling by the abstract memory management code.
  37. */
  38. #define FLT_IFETCH -1
  39. #define FLT_LOAD 0
  40. #define FLT_STORE 1
  41. /*
  42. * Canonical page fault handler
  43. */
  44. void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
  45. {
  46. struct vm_area_struct *vma;
  47. struct mm_struct *mm = current->mm;
  48. int si_signo;
  49. int si_code = SEGV_MAPERR;
  50. vm_fault_t fault;
  51. const struct exception_table_entry *fixup;
  52. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  53. /*
  54. * If we're in an interrupt or have no user context,
  55. * then must not take the fault.
  56. */
  57. if (unlikely(in_interrupt() || !mm))
  58. goto no_context;
  59. local_irq_enable();
  60. if (user_mode(regs))
  61. flags |= FAULT_FLAG_USER;
  62. retry:
  63. down_read(&mm->mmap_sem);
  64. vma = find_vma(mm, address);
  65. if (!vma)
  66. goto bad_area;
  67. if (vma->vm_start <= address)
  68. goto good_area;
  69. if (!(vma->vm_flags & VM_GROWSDOWN))
  70. goto bad_area;
  71. if (expand_stack(vma, address))
  72. goto bad_area;
  73. good_area:
  74. /* Address space is OK. Now check access rights. */
  75. si_code = SEGV_ACCERR;
  76. switch (cause) {
  77. case FLT_IFETCH:
  78. if (!(vma->vm_flags & VM_EXEC))
  79. goto bad_area;
  80. break;
  81. case FLT_LOAD:
  82. if (!(vma->vm_flags & VM_READ))
  83. goto bad_area;
  84. break;
  85. case FLT_STORE:
  86. if (!(vma->vm_flags & VM_WRITE))
  87. goto bad_area;
  88. flags |= FAULT_FLAG_WRITE;
  89. break;
  90. }
  91. fault = handle_mm_fault(vma, address, flags);
  92. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  93. return;
  94. /* The most common case -- we are done. */
  95. if (likely(!(fault & VM_FAULT_ERROR))) {
  96. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  97. if (fault & VM_FAULT_MAJOR)
  98. current->maj_flt++;
  99. else
  100. current->min_flt++;
  101. if (fault & VM_FAULT_RETRY) {
  102. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  103. flags |= FAULT_FLAG_TRIED;
  104. goto retry;
  105. }
  106. }
  107. up_read(&mm->mmap_sem);
  108. return;
  109. }
  110. up_read(&mm->mmap_sem);
  111. /* Handle copyin/out exception cases */
  112. if (!user_mode(regs))
  113. goto no_context;
  114. if (fault & VM_FAULT_OOM) {
  115. pagefault_out_of_memory();
  116. return;
  117. }
  118. /* User-mode address is in the memory map, but we are
  119. * unable to fix up the page fault.
  120. */
  121. if (fault & VM_FAULT_SIGBUS) {
  122. si_signo = SIGBUS;
  123. si_code = BUS_ADRERR;
  124. }
  125. /* Address is not in the memory map */
  126. else {
  127. si_signo = SIGSEGV;
  128. si_code = SEGV_ACCERR;
  129. }
  130. force_sig_fault(si_signo, si_code, (void __user *)address, current);
  131. return;
  132. bad_area:
  133. up_read(&mm->mmap_sem);
  134. if (user_mode(regs)) {
  135. force_sig_fault(SIGSEGV, si_code, (void __user *)address, current);
  136. return;
  137. }
  138. /* Kernel-mode fault falls through */
  139. no_context:
  140. fixup = search_exception_tables(pt_elr(regs));
  141. if (fixup) {
  142. pt_set_elr(regs, fixup->fixup);
  143. return;
  144. }
  145. /* Things are looking very, very bad now */
  146. bust_spinlocks(1);
  147. printk(KERN_EMERG "Unable to handle kernel paging request at "
  148. "virtual address 0x%08lx, regs %p\n", address, regs);
  149. die("Bad Kernel VA", regs, SIGKILL);
  150. }
  151. void read_protection_fault(struct pt_regs *regs)
  152. {
  153. unsigned long badvadr = pt_badva(regs);
  154. do_page_fault(badvadr, FLT_LOAD, regs);
  155. }
  156. void write_protection_fault(struct pt_regs *regs)
  157. {
  158. unsigned long badvadr = pt_badva(regs);
  159. do_page_fault(badvadr, FLT_STORE, regs);
  160. }
  161. void execute_protection_fault(struct pt_regs *regs)
  162. {
  163. unsigned long badvadr = pt_badva(regs);
  164. do_page_fault(badvadr, FLT_IFETCH, regs);
  165. }