vm_fault.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /*
  53. * If we're in an interrupt or have no user context,
  54. * then must not take the fault.
  55. */
  56. if (unlikely(in_interrupt() || !mm))
  57. goto no_context;
  58. local_irq_enable();
  59. if (user_mode(regs))
  60. flags |= FAULT_FLAG_USER;
  61. retry:
  62. down_read(&mm->mmap_sem);
  63. vma = find_vma(mm, address);
  64. if (!vma)
  65. goto bad_area;
  66. if (vma->vm_start <= address)
  67. goto good_area;
  68. if (!(vma->vm_flags & VM_GROWSDOWN))
  69. goto bad_area;
  70. if (expand_stack(vma, address))
  71. goto bad_area;
  72. good_area:
  73. /* Address space is OK. Now check access rights. */
  74. si_code = SEGV_ACCERR;
  75. switch (cause) {
  76. case FLT_IFETCH:
  77. if (!(vma->vm_flags & VM_EXEC))
  78. goto bad_area;
  79. break;
  80. case FLT_LOAD:
  81. if (!(vma->vm_flags & VM_READ))
  82. goto bad_area;
  83. break;
  84. case FLT_STORE:
  85. if (!(vma->vm_flags & VM_WRITE))
  86. goto bad_area;
  87. flags |= FAULT_FLAG_WRITE;
  88. break;
  89. }
  90. fault = handle_mm_fault(mm, vma, address, flags);
  91. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  92. return;
  93. /* The most common case -- we are done. */
  94. if (likely(!(fault & VM_FAULT_ERROR))) {
  95. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  96. if (fault & VM_FAULT_MAJOR)
  97. current->maj_flt++;
  98. else
  99. current->min_flt++;
  100. if (fault & VM_FAULT_RETRY) {
  101. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  102. flags |= FAULT_FLAG_TRIED;
  103. goto retry;
  104. }
  105. }
  106. up_read(&mm->mmap_sem);
  107. return;
  108. }
  109. up_read(&mm->mmap_sem);
  110. /* Handle copyin/out exception cases */
  111. if (!user_mode(regs))
  112. goto no_context;
  113. if (fault & VM_FAULT_OOM) {
  114. pagefault_out_of_memory();
  115. return;
  116. }
  117. /* User-mode address is in the memory map, but we are
  118. * unable to fix up the page fault.
  119. */
  120. if (fault & VM_FAULT_SIGBUS) {
  121. info.si_signo = SIGBUS;
  122. info.si_code = BUS_ADRERR;
  123. }
  124. /* Address is not in the memory map */
  125. else {
  126. info.si_signo = SIGSEGV;
  127. info.si_code = SEGV_ACCERR;
  128. }
  129. info.si_errno = 0;
  130. info.si_addr = (void __user *)address;
  131. force_sig_info(info.si_signo, &info, current);
  132. return;
  133. bad_area:
  134. up_read(&mm->mmap_sem);
  135. if (user_mode(regs)) {
  136. info.si_signo = SIGSEGV;
  137. info.si_errno = 0;
  138. info.si_code = si_code;
  139. info.si_addr = (void *)address;
  140. force_sig_info(info.si_signo, &info, current);
  141. return;
  142. }
  143. /* Kernel-mode fault falls through */
  144. no_context:
  145. fixup = search_exception_tables(pt_elr(regs));
  146. if (fixup) {
  147. pt_set_elr(regs, fixup->fixup);
  148. return;
  149. }
  150. /* Things are looking very, very bad now */
  151. bust_spinlocks(1);
  152. printk(KERN_EMERG "Unable to handle kernel paging request at "
  153. "virtual address 0x%08lx, regs %p\n", address, regs);
  154. die("Bad Kernel VA", regs, SIGKILL);
  155. }
  156. void read_protection_fault(struct pt_regs *regs)
  157. {
  158. unsigned long badvadr = pt_badva(regs);
  159. do_page_fault(badvadr, FLT_LOAD, regs);
  160. }
  161. void write_protection_fault(struct pt_regs *regs)
  162. {
  163. unsigned long badvadr = pt_badva(regs);
  164. do_page_fault(badvadr, FLT_STORE, regs);
  165. }
  166. void execute_protection_fault(struct pt_regs *regs)
  167. {
  168. unsigned long badvadr = pt_badva(regs);
  169. do_page_fault(badvadr, FLT_IFETCH, regs);
  170. }