vm_fault.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Memory fault handling for Hexagon
  3. *
  4. * Copyright (c) 2010-2011 Code Aurora Forum. 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. goto retry;
  101. }
  102. }
  103. up_read(&mm->mmap_sem);
  104. return;
  105. }
  106. up_read(&mm->mmap_sem);
  107. /* Handle copyin/out exception cases */
  108. if (!user_mode(regs))
  109. goto no_context;
  110. if (fault & VM_FAULT_OOM) {
  111. pagefault_out_of_memory();
  112. return;
  113. }
  114. /* User-mode address is in the memory map, but we are
  115. * unable to fix up the page fault.
  116. */
  117. if (fault & VM_FAULT_SIGBUS) {
  118. info.si_signo = SIGBUS;
  119. info.si_code = BUS_ADRERR;
  120. }
  121. /* Address is not in the memory map */
  122. else {
  123. info.si_signo = SIGSEGV;
  124. info.si_code = SEGV_ACCERR;
  125. }
  126. info.si_errno = 0;
  127. info.si_addr = (void __user *)address;
  128. force_sig_info(info.si_code, &info, current);
  129. return;
  130. bad_area:
  131. up_read(&mm->mmap_sem);
  132. if (user_mode(regs)) {
  133. info.si_signo = SIGSEGV;
  134. info.si_errno = 0;
  135. info.si_code = si_code;
  136. info.si_addr = (void *)address;
  137. force_sig_info(SIGSEGV, &info, current);
  138. return;
  139. }
  140. /* Kernel-mode fault falls through */
  141. no_context:
  142. fixup = search_exception_tables(pt_elr(regs));
  143. if (fixup) {
  144. pt_set_elr(regs, fixup->fixup);
  145. return;
  146. }
  147. /* Things are looking very, very bad now */
  148. bust_spinlocks(1);
  149. printk(KERN_EMERG "Unable to handle kernel paging request at "
  150. "virtual address 0x%08lx, regs %p\n", address, regs);
  151. die("Bad Kernel VA", regs, SIGKILL);
  152. }
  153. void read_protection_fault(struct pt_regs *regs)
  154. {
  155. unsigned long badvadr = pt_badva(regs);
  156. do_page_fault(badvadr, FLT_LOAD, regs);
  157. }
  158. void write_protection_fault(struct pt_regs *regs)
  159. {
  160. unsigned long badvadr = pt_badva(regs);
  161. do_page_fault(badvadr, FLT_STORE, regs);
  162. }
  163. void execute_protection_fault(struct pt_regs *regs)
  164. {
  165. unsigned long badvadr = pt_badva(regs);
  166. do_page_fault(badvadr, FLT_IFETCH, regs);
  167. }