/arch/xtensa/mm/fault.c

http://github.com/mirrors/linux · C · 266 lines · 170 code · 42 blank · 54 comment · 45 complexity · 3bdc1f535b9b1bd894f83abc0aabaa8f MD5 · raw file

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