fault.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // TODO VM_EXEC flag work-around, cache aliasing
  2. /*
  3. * arch/xtensa/mm/fault.c
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * Copyright (C) 2001 - 2010 Tensilica Inc.
  10. *
  11. * Chris Zankel <chris@zankel.net>
  12. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/extable.h>
  16. #include <linux/hardirq.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/hardirq.h>
  22. #include <asm/traps.h>
  23. void bad_page_fault(struct pt_regs*, unsigned long, int);
  24. static void vmalloc_fault(struct pt_regs *regs, unsigned int address)
  25. {
  26. #ifdef CONFIG_MMU
  27. /* Synchronize this task's top level page-table
  28. * with the 'reference' page table.
  29. */
  30. struct mm_struct *act_mm = current->active_mm;
  31. int index = pgd_index(address);
  32. pgd_t *pgd, *pgd_k;
  33. p4d_t *p4d, *p4d_k;
  34. pud_t *pud, *pud_k;
  35. pmd_t *pmd, *pmd_k;
  36. pte_t *pte_k;
  37. if (act_mm == NULL)
  38. goto bad_page_fault;
  39. pgd = act_mm->pgd + index;
  40. pgd_k = init_mm.pgd + index;
  41. if (!pgd_present(*pgd_k))
  42. goto bad_page_fault;
  43. pgd_val(*pgd) = pgd_val(*pgd_k);
  44. p4d = p4d_offset(pgd, address);
  45. p4d_k = p4d_offset(pgd_k, address);
  46. if (!p4d_present(*p4d) || !p4d_present(*p4d_k))
  47. goto bad_page_fault;
  48. pud = pud_offset(p4d, address);
  49. pud_k = pud_offset(p4d_k, address);
  50. if (!pud_present(*pud) || !pud_present(*pud_k))
  51. goto bad_page_fault;
  52. pmd = pmd_offset(pud, address);
  53. pmd_k = pmd_offset(pud_k, address);
  54. if (!pmd_present(*pmd) || !pmd_present(*pmd_k))
  55. goto bad_page_fault;
  56. pmd_val(*pmd) = pmd_val(*pmd_k);
  57. pte_k = pte_offset_kernel(pmd_k, address);
  58. if (!pte_present(*pte_k))
  59. goto bad_page_fault;
  60. return;
  61. bad_page_fault:
  62. bad_page_fault(regs, address, SIGKILL);
  63. #else
  64. WARN_ONCE(1, "%s in noMMU configuration\n", __func__);
  65. #endif
  66. }
  67. /*
  68. * This routine handles page faults. It determines the address,
  69. * and the problem, and then passes it off to one of the appropriate
  70. * routines.
  71. *
  72. * Note: does not handle Miss and MultiHit.
  73. */
  74. void do_page_fault(struct pt_regs *regs)
  75. {
  76. struct vm_area_struct * vma;
  77. struct mm_struct *mm = current->mm;
  78. unsigned int exccause = regs->exccause;
  79. unsigned int address = regs->excvaddr;
  80. int code;
  81. int is_write, is_exec;
  82. vm_fault_t fault;
  83. unsigned int flags = FAULT_FLAG_DEFAULT;
  84. code = SEGV_MAPERR;
  85. /* We fault-in kernel-space virtual memory on-demand. The
  86. * 'reference' page table is init_mm.pgd.
  87. */
  88. if (address >= TASK_SIZE && !user_mode(regs)) {
  89. vmalloc_fault(regs, address);
  90. return;
  91. }
  92. /* If we're in an interrupt or have no user
  93. * context, we must not take the fault..
  94. */
  95. if (faulthandler_disabled() || !mm) {
  96. bad_page_fault(regs, address, SIGSEGV);
  97. return;
  98. }
  99. is_write = (exccause == EXCCAUSE_STORE_CACHE_ATTRIBUTE) ? 1 : 0;
  100. is_exec = (exccause == EXCCAUSE_ITLB_PRIVILEGE ||
  101. exccause == EXCCAUSE_ITLB_MISS ||
  102. exccause == EXCCAUSE_FETCH_CACHE_ATTRIBUTE) ? 1 : 0;
  103. pr_debug("[%s:%d:%08x:%d:%08lx:%s%s]\n",
  104. current->comm, current->pid,
  105. address, exccause, regs->pc,
  106. is_write ? "w" : "", is_exec ? "x" : "");
  107. if (user_mode(regs))
  108. flags |= FAULT_FLAG_USER;
  109. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  110. retry:
  111. vma = lock_mm_and_find_vma(mm, address, regs);
  112. if (!vma)
  113. goto bad_area_nosemaphore;
  114. /* Ok, we have a good vm_area for this memory access, so
  115. * we can handle it..
  116. */
  117. code = SEGV_ACCERR;
  118. if (is_write) {
  119. if (!(vma->vm_flags & VM_WRITE))
  120. goto bad_area;
  121. flags |= FAULT_FLAG_WRITE;
  122. } else if (is_exec) {
  123. if (!(vma->vm_flags & VM_EXEC))
  124. goto bad_area;
  125. } else /* Allow read even from write-only pages. */
  126. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  127. goto bad_area;
  128. /* If for any reason at all we couldn't handle the fault,
  129. * make sure we exit gracefully rather than endlessly redo
  130. * the fault.
  131. */
  132. fault = handle_mm_fault(vma, address, flags, regs);
  133. if (fault_signal_pending(fault, regs)) {
  134. if (!user_mode(regs))
  135. bad_page_fault(regs, address, SIGKILL);
  136. return;
  137. }
  138. /* The fault is fully completed (including releasing mmap lock) */
  139. if (fault & VM_FAULT_COMPLETED)
  140. return;
  141. if (unlikely(fault & VM_FAULT_ERROR)) {
  142. if (fault & VM_FAULT_OOM)
  143. goto out_of_memory;
  144. else if (fault & VM_FAULT_SIGSEGV)
  145. goto bad_area;
  146. else if (fault & VM_FAULT_SIGBUS)
  147. goto do_sigbus;
  148. BUG();
  149. }
  150. if (fault & VM_FAULT_RETRY) {
  151. flags |= FAULT_FLAG_TRIED;
  152. /* No need to mmap_read_unlock(mm) as we would
  153. * have already released it in __lock_page_or_retry
  154. * in mm/filemap.c.
  155. */
  156. goto retry;
  157. }
  158. mmap_read_unlock(mm);
  159. return;
  160. /* Something tried to access memory that isn't in our memory map..
  161. * Fix it, but check if it's kernel or user first..
  162. */
  163. bad_area:
  164. mmap_read_unlock(mm);
  165. bad_area_nosemaphore:
  166. if (user_mode(regs)) {
  167. force_sig_fault(SIGSEGV, code, (void *) address);
  168. return;
  169. }
  170. bad_page_fault(regs, address, SIGSEGV);
  171. return;
  172. /* We ran out of memory, or some other thing happened to us that made
  173. * us unable to handle the page fault gracefully.
  174. */
  175. out_of_memory:
  176. mmap_read_unlock(mm);
  177. if (!user_mode(regs))
  178. bad_page_fault(regs, address, SIGKILL);
  179. else
  180. pagefault_out_of_memory();
  181. return;
  182. do_sigbus:
  183. mmap_read_unlock(mm);
  184. /* Send a sigbus, regardless of whether we were in kernel
  185. * or user mode.
  186. */
  187. force_sig_fault(SIGBUS, BUS_ADRERR, (void *) address);
  188. /* Kernel mode? Handle exceptions or die */
  189. if (!user_mode(regs))
  190. bad_page_fault(regs, address, SIGBUS);
  191. return;
  192. }
  193. void
  194. bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  195. {
  196. extern void __noreturn die(const char*, struct pt_regs*, long);
  197. const struct exception_table_entry *entry;
  198. /* Are we prepared to handle this kernel fault? */
  199. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  200. pr_debug("%s: Exception at pc=%#010lx (%lx)\n",
  201. current->comm, regs->pc, entry->fixup);
  202. regs->pc = entry->fixup;
  203. return;
  204. }
  205. /* Oops. The kernel tried to access some bad page. We'll have to
  206. * terminate things with extreme prejudice.
  207. */
  208. pr_alert("Unable to handle kernel paging request at virtual "
  209. "address %08lx\n pc = %08lx, ra = %08lx\n",
  210. address, regs->pc, regs->areg[0]);
  211. die("Oops", regs, sig);
  212. }