fault.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <asm/mmu_context.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/hardirq.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgalloc.h>
  22. unsigned long asid_cache = ASID_USER_FIRST;
  23. void bad_page_fault(struct pt_regs*, unsigned long, int);
  24. #undef DEBUG_PAGE_FAULT
  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. siginfo_t info;
  39. int is_write, is_exec;
  40. int fault;
  41. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  42. info.si_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 (in_atomic() || !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. #ifdef DEBUG_PAGE_FAULT
  60. printk("[%s:%d:%08x:%d:%08x:%s%s]\n", current->comm, current->pid,
  61. address, exccause, regs->pc, is_write? "w":"", is_exec? "x":"");
  62. #endif
  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. info.si_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(mm, vma, address, flags);
  96. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  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_SIGBUS)
  102. goto do_sigbus;
  103. BUG();
  104. }
  105. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  106. if (fault & VM_FAULT_MAJOR)
  107. current->maj_flt++;
  108. else
  109. current->min_flt++;
  110. if (fault & VM_FAULT_RETRY) {
  111. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  112. flags |= FAULT_FLAG_TRIED;
  113. /* No need to up_read(&mm->mmap_sem) as we would
  114. * have already released it in __lock_page_or_retry
  115. * in mm/filemap.c.
  116. */
  117. goto retry;
  118. }
  119. }
  120. up_read(&mm->mmap_sem);
  121. return;
  122. /* Something tried to access memory that isn't in our memory map..
  123. * Fix it, but check if it's kernel or user first..
  124. */
  125. bad_area:
  126. up_read(&mm->mmap_sem);
  127. if (user_mode(regs)) {
  128. current->thread.bad_vaddr = address;
  129. current->thread.error_code = is_write;
  130. info.si_signo = SIGSEGV;
  131. info.si_errno = 0;
  132. /* info.si_code has been set above */
  133. info.si_addr = (void *) address;
  134. force_sig_info(SIGSEGV, &info, current);
  135. return;
  136. }
  137. bad_page_fault(regs, address, SIGSEGV);
  138. return;
  139. /* We ran out of memory, or some other thing happened to us that made
  140. * us unable to handle the page fault gracefully.
  141. */
  142. out_of_memory:
  143. up_read(&mm->mmap_sem);
  144. if (!user_mode(regs))
  145. bad_page_fault(regs, address, SIGKILL);
  146. else
  147. pagefault_out_of_memory();
  148. return;
  149. do_sigbus:
  150. up_read(&mm->mmap_sem);
  151. /* Send a sigbus, regardless of whether we were in kernel
  152. * or user mode.
  153. */
  154. current->thread.bad_vaddr = address;
  155. info.si_code = SIGBUS;
  156. info.si_errno = 0;
  157. info.si_code = BUS_ADRERR;
  158. info.si_addr = (void *) address;
  159. force_sig_info(SIGBUS, &info, current);
  160. /* Kernel mode? Handle exceptions or die */
  161. if (!user_mode(regs))
  162. bad_page_fault(regs, address, SIGBUS);
  163. return;
  164. vmalloc_fault:
  165. {
  166. /* Synchronize this task's top level page-table
  167. * with the 'reference' page table.
  168. */
  169. struct mm_struct *act_mm = current->active_mm;
  170. int index = pgd_index(address);
  171. pgd_t *pgd, *pgd_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. pmd = pmd_offset(pgd, address);
  182. pmd_k = pmd_offset(pgd_k, address);
  183. if (!pmd_present(*pmd) || !pmd_present(*pmd_k))
  184. goto bad_page_fault;
  185. pmd_val(*pmd) = pmd_val(*pmd_k);
  186. pte_k = pte_offset_kernel(pmd_k, address);
  187. if (!pte_present(*pte_k))
  188. goto bad_page_fault;
  189. return;
  190. }
  191. bad_page_fault:
  192. bad_page_fault(regs, address, SIGKILL);
  193. return;
  194. }
  195. void
  196. bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  197. {
  198. extern void 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. #ifdef DEBUG_PAGE_FAULT
  203. printk(KERN_DEBUG "%s: Exception at pc=%#010lx (%lx)\n",
  204. current->comm, regs->pc, entry->fixup);
  205. #endif
  206. current->thread.bad_uaddr = address;
  207. regs->pc = entry->fixup;
  208. return;
  209. }
  210. /* Oops. The kernel tried to access some bad page. We'll have to
  211. * terminate things with extreme prejudice.
  212. */
  213. printk(KERN_ALERT "Unable to handle kernel paging request at virtual "
  214. "address %08lx\n pc = %08lx, ra = %08lx\n",
  215. address, regs->pc, regs->areg[0]);
  216. die("Oops", regs, sig);
  217. do_exit(sig);
  218. }