fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 - 2005 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 <asm/mmu_context.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/hardirq.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/system.h>
  21. #include <asm/pgalloc.h>
  22. unsigned long asid_cache = ASID_FIRST_VERSION;
  23. void bad_page_fault(struct pt_regs*, unsigned long, int);
  24. /*
  25. * This routine handles page faults. It determines the address,
  26. * and the problem, and then passes it off to one of the appropriate
  27. * routines.
  28. *
  29. * Note: does not handle Miss and MultiHit.
  30. */
  31. void do_page_fault(struct pt_regs *regs)
  32. {
  33. struct vm_area_struct * vma;
  34. struct mm_struct *mm = current->mm;
  35. unsigned int exccause = regs->exccause;
  36. unsigned int address = regs->excvaddr;
  37. siginfo_t info;
  38. int is_write, is_exec;
  39. info.si_code = SEGV_MAPERR;
  40. /* We fault-in kernel-space virtual memory on-demand. The
  41. * 'reference' page table is init_mm.pgd.
  42. */
  43. if (address >= TASK_SIZE && !user_mode(regs))
  44. goto vmalloc_fault;
  45. /* If we're in an interrupt or have no user
  46. * context, we must not take the fault..
  47. */
  48. if (in_atomic() || !mm) {
  49. bad_page_fault(regs, address, SIGSEGV);
  50. return;
  51. }
  52. is_write = (exccause == XCHAL_EXCCAUSE_STORE_CACHE_ATTRIBUTE) ? 1 : 0;
  53. is_exec = (exccause == XCHAL_EXCCAUSE_ITLB_PRIVILEGE ||
  54. exccause == XCHAL_EXCCAUSE_ITLB_MISS ||
  55. exccause == XCHAL_EXCCAUSE_FETCH_CACHE_ATTRIBUTE) ? 1 : 0;
  56. #if 0
  57. printk("[%s:%d:%08x:%d:%08x:%s%s]\n", current->comm, current->pid,
  58. address, exccause, regs->pc, is_write? "w":"", is_exec? "x":"");
  59. #endif
  60. down_read(&mm->mmap_sem);
  61. vma = find_vma(mm, address);
  62. if (!vma)
  63. goto bad_area;
  64. if (vma->vm_start <= address)
  65. goto good_area;
  66. if (!(vma->vm_flags & VM_GROWSDOWN))
  67. goto bad_area;
  68. if (expand_stack(vma, address))
  69. goto bad_area;
  70. /* Ok, we have a good vm_area for this memory access, so
  71. * we can handle it..
  72. */
  73. good_area:
  74. info.si_code = SEGV_ACCERR;
  75. if (is_write) {
  76. if (!(vma->vm_flags & VM_WRITE))
  77. goto bad_area;
  78. } else if (is_exec) {
  79. if (!(vma->vm_flags & VM_EXEC))
  80. goto bad_area;
  81. } else /* Allow read even from write-only pages. */
  82. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  83. goto bad_area;
  84. /* If for any reason at all we couldn't handle the fault,
  85. * make sure we exit gracefully rather than endlessly redo
  86. * the fault.
  87. */
  88. survive:
  89. switch (handle_mm_fault(mm, vma, address, is_write)) {
  90. case VM_FAULT_MINOR:
  91. current->min_flt++;
  92. break;
  93. case VM_FAULT_MAJOR:
  94. current->maj_flt++;
  95. break;
  96. case VM_FAULT_SIGBUS:
  97. goto do_sigbus;
  98. case VM_FAULT_OOM:
  99. goto out_of_memory;
  100. default:
  101. BUG();
  102. }
  103. up_read(&mm->mmap_sem);
  104. return;
  105. /* Something tried to access memory that isn't in our memory map..
  106. * Fix it, but check if it's kernel or user first..
  107. */
  108. bad_area:
  109. up_read(&mm->mmap_sem);
  110. if (user_mode(regs)) {
  111. current->thread.bad_vaddr = address;
  112. current->thread.error_code = is_write;
  113. info.si_signo = SIGSEGV;
  114. info.si_errno = 0;
  115. /* info.si_code has been set above */
  116. info.si_addr = (void *) address;
  117. force_sig_info(SIGSEGV, &info, current);
  118. return;
  119. }
  120. bad_page_fault(regs, address, SIGSEGV);
  121. return;
  122. /* We ran out of memory, or some other thing happened to us that made
  123. * us unable to handle the page fault gracefully.
  124. */
  125. out_of_memory:
  126. up_read(&mm->mmap_sem);
  127. if (current->pid == 1) {
  128. yield();
  129. down_read(&mm->mmap_sem);
  130. goto survive;
  131. }
  132. printk("VM: killing process %s\n", current->comm);
  133. if (user_mode(regs))
  134. do_exit(SIGKILL);
  135. bad_page_fault(regs, address, SIGKILL);
  136. return;
  137. do_sigbus:
  138. up_read(&mm->mmap_sem);
  139. /* Send a sigbus, regardless of whether we were in kernel
  140. * or user mode.
  141. */
  142. current->thread.bad_vaddr = address;
  143. info.si_code = SIGBUS;
  144. info.si_errno = 0;
  145. info.si_code = BUS_ADRERR;
  146. info.si_addr = (void *) address;
  147. force_sig_info(SIGBUS, &info, current);
  148. /* Kernel mode? Handle exceptions or die */
  149. if (!user_mode(regs))
  150. bad_page_fault(regs, address, SIGBUS);
  151. vmalloc_fault:
  152. {
  153. /* Synchronize this task's top level page-table
  154. * with the 'reference' page table.
  155. */
  156. struct mm_struct *act_mm = current->active_mm;
  157. int index = pgd_index(address);
  158. pgd_t *pgd, *pgd_k;
  159. pmd_t *pmd, *pmd_k;
  160. pte_t *pte_k;
  161. if (act_mm == NULL)
  162. goto bad_page_fault;
  163. pgd = act_mm->pgd + index;
  164. pgd_k = init_mm.pgd + index;
  165. if (!pgd_present(*pgd_k))
  166. goto bad_page_fault;
  167. pgd_val(*pgd) = pgd_val(*pgd_k);
  168. pmd = pmd_offset(pgd, address);
  169. pmd_k = pmd_offset(pgd_k, address);
  170. if (!pmd_present(*pmd) || !pmd_present(*pmd_k))
  171. goto bad_page_fault;
  172. pmd_val(*pmd) = pmd_val(*pmd_k);
  173. pte_k = pte_offset_kernel(pmd_k, address);
  174. if (!pte_present(*pte_k))
  175. goto bad_page_fault;
  176. return;
  177. }
  178. bad_page_fault:
  179. bad_page_fault(regs, address, SIGKILL);
  180. return;
  181. }
  182. void
  183. bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  184. {
  185. extern void die(const char*, struct pt_regs*, long);
  186. const struct exception_table_entry *entry;
  187. /* Are we prepared to handle this kernel fault? */
  188. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  189. #if 1
  190. printk(KERN_DEBUG "%s: Exception at pc=%#010lx (%lx)\n",
  191. current->comm, regs->pc, entry->fixup);
  192. #endif
  193. current->thread.bad_uaddr = address;
  194. regs->pc = entry->fixup;
  195. return;
  196. }
  197. /* Oops. The kernel tried to access some bad page. We'll have to
  198. * terminate things with extreme prejudice.
  199. */
  200. printk(KERN_ALERT "Unable to handle kernel paging request at virtual "
  201. "address %08lx\n pc = %08lx, ra = %08lx\n",
  202. address, regs->pc, regs->areg[0]);
  203. die("Oops", regs, sig);
  204. do_exit(sig);
  205. }