fault.c 6.3 KB

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