vm_fault.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <asm/uaccess.h>
  28. #include <linux/mm.h>
  29. #include <linux/signal.h>
  30. #include <linux/module.h>
  31. #include <linux/hardirq.h>
  32. /*
  33. * Decode of hardware exception sends us to one of several
  34. * entry points. At each, we generate canonical arguments
  35. * for handling by the abstract memory management code.
  36. */
  37. #define FLT_IFETCH -1
  38. #define FLT_LOAD 0
  39. #define FLT_STORE 1
  40. /*
  41. * Canonical page fault handler
  42. */
  43. void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
  44. {
  45. struct vm_area_struct *vma;
  46. struct mm_struct *mm = current->mm;
  47. siginfo_t info;
  48. int si_code = SEGV_MAPERR;
  49. int fault;
  50. const struct exception_table_entry *fixup;
  51. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  52. (cause > 0 ? FAULT_FLAG_WRITE : 0);
  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. retry:
  61. down_read(&mm->mmap_sem);
  62. vma = find_vma(mm, address);
  63. if (!vma)
  64. goto bad_area;
  65. if (vma->vm_start <= address)
  66. goto good_area;
  67. if (!(vma->vm_flags & VM_GROWSDOWN))
  68. goto bad_area;
  69. if (expand_stack(vma, address))
  70. goto bad_area;
  71. good_area:
  72. /* Address space is OK. Now check access rights. */
  73. si_code = SEGV_ACCERR;
  74. switch (cause) {
  75. case FLT_IFETCH:
  76. if (!(vma->vm_flags & VM_EXEC))
  77. goto bad_area;
  78. break;
  79. case FLT_LOAD:
  80. if (!(vma->vm_flags & VM_READ))
  81. goto bad_area;
  82. break;
  83. case FLT_STORE:
  84. if (!(vma->vm_flags & VM_WRITE))
  85. goto bad_area;
  86. break;
  87. }
  88. fault = handle_mm_fault(mm, vma, address, flags);
  89. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  90. return;
  91. /* The most common case -- we are done. */
  92. if (likely(!(fault & VM_FAULT_ERROR))) {
  93. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  94. if (fault & VM_FAULT_MAJOR)
  95. current->maj_flt++;
  96. else
  97. current->min_flt++;
  98. if (fault & VM_FAULT_RETRY) {
  99. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  100. flags |= FAULT_FLAG_TRIED;
  101. goto retry;
  102. }
  103. }
  104. up_read(&mm->mmap_sem);
  105. return;
  106. }
  107. up_read(&mm->mmap_sem);
  108. /* Handle copyin/out exception cases */
  109. if (!user_mode(regs))
  110. goto no_context;
  111. if (fault & VM_FAULT_OOM) {
  112. pagefault_out_of_memory();
  113. return;
  114. }
  115. /* User-mode address is in the memory map, but we are
  116. * unable to fix up the page fault.
  117. */
  118. if (fault & VM_FAULT_SIGBUS) {
  119. info.si_signo = SIGBUS;
  120. info.si_code = BUS_ADRERR;
  121. }
  122. /* Address is not in the memory map */
  123. else {
  124. info.si_signo = SIGSEGV;
  125. info.si_code = SEGV_ACCERR;
  126. }
  127. info.si_errno = 0;
  128. info.si_addr = (void __user *)address;
  129. force_sig_info(info.si_code, &info, current);
  130. return;
  131. bad_area:
  132. up_read(&mm->mmap_sem);
  133. if (user_mode(regs)) {
  134. info.si_signo = SIGSEGV;
  135. info.si_errno = 0;
  136. info.si_code = si_code;
  137. info.si_addr = (void *)address;
  138. force_sig_info(SIGSEGV, &info, current);
  139. return;
  140. }
  141. /* Kernel-mode fault falls through */
  142. no_context:
  143. fixup = search_exception_tables(pt_elr(regs));
  144. if (fixup) {
  145. pt_set_elr(regs, fixup->fixup);
  146. return;
  147. }
  148. /* Things are looking very, very bad now */
  149. bust_spinlocks(1);
  150. printk(KERN_EMERG "Unable to handle kernel paging request at "
  151. "virtual address 0x%08lx, regs %p\n", address, regs);
  152. die("Bad Kernel VA", regs, SIGKILL);
  153. }
  154. void read_protection_fault(struct pt_regs *regs)
  155. {
  156. unsigned long badvadr = pt_badva(regs);
  157. do_page_fault(badvadr, FLT_LOAD, regs);
  158. }
  159. void write_protection_fault(struct pt_regs *regs)
  160. {
  161. unsigned long badvadr = pt_badva(regs);
  162. do_page_fault(badvadr, FLT_STORE, regs);
  163. }
  164. void execute_protection_fault(struct pt_regs *regs)
  165. {
  166. unsigned long badvadr = pt_badva(regs);
  167. do_page_fault(badvadr, FLT_IFETCH, regs);
  168. }