fault.c 6.6 KB

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