fault.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * Based on linux/arch/sh/mm/fault.c:
  5. * Copyright (C) 1999 Niibe Yutaka
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/extable.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/sysreg.h>
  19. #include <asm/tlb.h>
  20. #ifdef CONFIG_KPROBES
  21. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  22. {
  23. int ret = 0;
  24. if (!user_mode(regs)) {
  25. if (kprobe_running() && kprobe_fault_handler(regs, trap))
  26. ret = 1;
  27. }
  28. return ret;
  29. }
  30. #else
  31. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  32. {
  33. return 0;
  34. }
  35. #endif
  36. int exception_trace = 1;
  37. /*
  38. * This routine handles page faults. It determines the address and the
  39. * problem, and then passes it off to one of the appropriate routines.
  40. *
  41. * ecr is the Exception Cause Register. Possible values are:
  42. * 6: Protection fault (instruction access)
  43. * 15: Protection fault (read access)
  44. * 16: Protection fault (write access)
  45. * 20: Page not found (instruction access)
  46. * 24: Page not found (read access)
  47. * 28: Page not found (write access)
  48. */
  49. asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
  50. {
  51. struct task_struct *tsk;
  52. struct mm_struct *mm;
  53. struct vm_area_struct *vma;
  54. const struct exception_table_entry *fixup;
  55. unsigned long address;
  56. unsigned long page;
  57. long signr;
  58. int code;
  59. int fault;
  60. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  61. if (notify_page_fault(regs, ecr))
  62. return;
  63. address = sysreg_read(TLBEAR);
  64. tsk = current;
  65. mm = tsk->mm;
  66. signr = SIGSEGV;
  67. code = SEGV_MAPERR;
  68. /*
  69. * If we're in an interrupt or have no user context, we must
  70. * not take the fault...
  71. */
  72. if (faulthandler_disabled() || !mm || regs->sr & SYSREG_BIT(GM))
  73. goto no_context;
  74. local_irq_enable();
  75. if (user_mode(regs))
  76. flags |= FAULT_FLAG_USER;
  77. retry:
  78. down_read(&mm->mmap_sem);
  79. vma = find_vma(mm, address);
  80. if (!vma)
  81. goto bad_area;
  82. if (vma->vm_start <= address)
  83. goto good_area;
  84. if (!(vma->vm_flags & VM_GROWSDOWN))
  85. goto bad_area;
  86. if (expand_stack(vma, address))
  87. goto bad_area;
  88. /*
  89. * Ok, we have a good vm_area for this memory access, so we
  90. * can handle it...
  91. */
  92. good_area:
  93. code = SEGV_ACCERR;
  94. switch (ecr) {
  95. case ECR_PROTECTION_X:
  96. case ECR_TLB_MISS_X:
  97. if (!(vma->vm_flags & VM_EXEC))
  98. goto bad_area;
  99. break;
  100. case ECR_PROTECTION_R:
  101. case ECR_TLB_MISS_R:
  102. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  103. goto bad_area;
  104. break;
  105. case ECR_PROTECTION_W:
  106. case ECR_TLB_MISS_W:
  107. if (!(vma->vm_flags & VM_WRITE))
  108. goto bad_area;
  109. flags |= FAULT_FLAG_WRITE;
  110. break;
  111. default:
  112. panic("Unhandled case %lu in do_page_fault!", ecr);
  113. }
  114. /*
  115. * If for any reason at all we couldn't handle the fault, make
  116. * sure we exit gracefully rather than endlessly redo the
  117. * fault.
  118. */
  119. fault = handle_mm_fault(vma, address, flags);
  120. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  121. return;
  122. if (unlikely(fault & VM_FAULT_ERROR)) {
  123. if (fault & VM_FAULT_OOM)
  124. goto out_of_memory;
  125. else if (fault & VM_FAULT_SIGSEGV)
  126. goto bad_area;
  127. else if (fault & VM_FAULT_SIGBUS)
  128. goto do_sigbus;
  129. BUG();
  130. }
  131. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  132. if (fault & VM_FAULT_MAJOR)
  133. tsk->maj_flt++;
  134. else
  135. tsk->min_flt++;
  136. if (fault & VM_FAULT_RETRY) {
  137. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  138. flags |= FAULT_FLAG_TRIED;
  139. /*
  140. * No need to up_read(&mm->mmap_sem) as we would have
  141. * already released it in __lock_page_or_retry() in
  142. * mm/filemap.c.
  143. */
  144. goto retry;
  145. }
  146. }
  147. up_read(&mm->mmap_sem);
  148. return;
  149. /*
  150. * Something tried to access memory that isn't in our memory
  151. * map. Fix it, but check if it's kernel or user first...
  152. */
  153. bad_area:
  154. up_read(&mm->mmap_sem);
  155. if (user_mode(regs)) {
  156. if (exception_trace && printk_ratelimit())
  157. printk("%s%s[%d]: segfault at %08lx pc %08lx "
  158. "sp %08lx ecr %lu\n",
  159. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  160. tsk->comm, tsk->pid, address, regs->pc,
  161. regs->sp, ecr);
  162. _exception(SIGSEGV, regs, code, address);
  163. return;
  164. }
  165. no_context:
  166. /* Are we prepared to handle this kernel fault? */
  167. fixup = search_exception_tables(regs->pc);
  168. if (fixup) {
  169. regs->pc = fixup->fixup;
  170. return;
  171. }
  172. /*
  173. * Oops. The kernel tried to access some bad page. We'll have
  174. * to terminate things with extreme prejudice.
  175. */
  176. if (address < PAGE_SIZE)
  177. printk(KERN_ALERT
  178. "Unable to handle kernel NULL pointer dereference");
  179. else
  180. printk(KERN_ALERT
  181. "Unable to handle kernel paging request");
  182. printk(" at virtual address %08lx\n", address);
  183. page = sysreg_read(PTBR);
  184. printk(KERN_ALERT "ptbr = %08lx", page);
  185. if (address >= TASK_SIZE)
  186. page = (unsigned long)swapper_pg_dir;
  187. if (page) {
  188. page = ((unsigned long *)page)[address >> 22];
  189. printk(" pgd = %08lx", page);
  190. if (page & _PAGE_PRESENT) {
  191. page &= PAGE_MASK;
  192. address &= 0x003ff000;
  193. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  194. printk(" pte = %08lx", page);
  195. }
  196. }
  197. printk("\n");
  198. die("Kernel access of bad area", regs, signr);
  199. return;
  200. /*
  201. * We ran out of memory, or some other thing happened to us
  202. * that made us unable to handle the page fault gracefully.
  203. */
  204. out_of_memory:
  205. up_read(&mm->mmap_sem);
  206. if (!user_mode(regs))
  207. goto no_context;
  208. pagefault_out_of_memory();
  209. return;
  210. do_sigbus:
  211. up_read(&mm->mmap_sem);
  212. /* Kernel mode? Handle exceptions or die */
  213. signr = SIGBUS;
  214. code = BUS_ADRERR;
  215. if (!user_mode(regs))
  216. goto no_context;
  217. if (exception_trace)
  218. printk("%s%s[%d]: bus error at %08lx pc %08lx "
  219. "sp %08lx ecr %lu\n",
  220. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  221. tsk->comm, tsk->pid, address, regs->pc,
  222. regs->sp, ecr);
  223. _exception(SIGBUS, regs, BUS_ADRERR, address);
  224. }
  225. asmlinkage void do_bus_error(unsigned long addr, int write_access,
  226. struct pt_regs *regs)
  227. {
  228. printk(KERN_ALERT
  229. "Bus error at physical address 0x%08lx (%s access)\n",
  230. addr, write_access ? "write" : "read");
  231. printk(KERN_INFO "DTLB dump:\n");
  232. dump_dtlb();
  233. die("Bus Error", regs, SIGKILL);
  234. }