fault.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/module.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/kprobes.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/sysreg.h>
  18. #include <asm/tlb.h>
  19. #include <asm/uaccess.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 (in_atomic() || !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(mm, 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_SIGBUS)
  126. goto do_sigbus;
  127. BUG();
  128. }
  129. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  130. if (fault & VM_FAULT_MAJOR)
  131. tsk->maj_flt++;
  132. else
  133. tsk->min_flt++;
  134. if (fault & VM_FAULT_RETRY) {
  135. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  136. flags |= FAULT_FLAG_TRIED;
  137. /*
  138. * No need to up_read(&mm->mmap_sem) as we would have
  139. * already released it in __lock_page_or_retry() in
  140. * mm/filemap.c.
  141. */
  142. goto retry;
  143. }
  144. }
  145. up_read(&mm->mmap_sem);
  146. return;
  147. /*
  148. * Something tried to access memory that isn't in our memory
  149. * map. Fix it, but check if it's kernel or user first...
  150. */
  151. bad_area:
  152. up_read(&mm->mmap_sem);
  153. if (user_mode(regs)) {
  154. if (exception_trace && printk_ratelimit())
  155. printk("%s%s[%d]: segfault at %08lx pc %08lx "
  156. "sp %08lx ecr %lu\n",
  157. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  158. tsk->comm, tsk->pid, address, regs->pc,
  159. regs->sp, ecr);
  160. _exception(SIGSEGV, regs, code, address);
  161. return;
  162. }
  163. no_context:
  164. /* Are we prepared to handle this kernel fault? */
  165. fixup = search_exception_tables(regs->pc);
  166. if (fixup) {
  167. regs->pc = fixup->fixup;
  168. return;
  169. }
  170. /*
  171. * Oops. The kernel tried to access some bad page. We'll have
  172. * to terminate things with extreme prejudice.
  173. */
  174. if (address < PAGE_SIZE)
  175. printk(KERN_ALERT
  176. "Unable to handle kernel NULL pointer dereference");
  177. else
  178. printk(KERN_ALERT
  179. "Unable to handle kernel paging request");
  180. printk(" at virtual address %08lx\n", address);
  181. page = sysreg_read(PTBR);
  182. printk(KERN_ALERT "ptbr = %08lx", page);
  183. if (address >= TASK_SIZE)
  184. page = (unsigned long)swapper_pg_dir;
  185. if (page) {
  186. page = ((unsigned long *)page)[address >> 22];
  187. printk(" pgd = %08lx", page);
  188. if (page & _PAGE_PRESENT) {
  189. page &= PAGE_MASK;
  190. address &= 0x003ff000;
  191. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  192. printk(" pte = %08lx", page);
  193. }
  194. }
  195. printk("\n");
  196. die("Kernel access of bad area", regs, signr);
  197. return;
  198. /*
  199. * We ran out of memory, or some other thing happened to us
  200. * that made us unable to handle the page fault gracefully.
  201. */
  202. out_of_memory:
  203. up_read(&mm->mmap_sem);
  204. if (!user_mode(regs))
  205. goto no_context;
  206. pagefault_out_of_memory();
  207. return;
  208. do_sigbus:
  209. up_read(&mm->mmap_sem);
  210. /* Kernel mode? Handle exceptions or die */
  211. signr = SIGBUS;
  212. code = BUS_ADRERR;
  213. if (!user_mode(regs))
  214. goto no_context;
  215. if (exception_trace)
  216. printk("%s%s[%d]: bus error at %08lx pc %08lx "
  217. "sp %08lx ecr %lu\n",
  218. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  219. tsk->comm, tsk->pid, address, regs->pc,
  220. regs->sp, ecr);
  221. _exception(SIGBUS, regs, BUS_ADRERR, address);
  222. }
  223. asmlinkage void do_bus_error(unsigned long addr, int write_access,
  224. struct pt_regs *regs)
  225. {
  226. printk(KERN_ALERT
  227. "Bus error at physical address 0x%08lx (%s access)\n",
  228. addr, write_access ? "write" : "read");
  229. printk(KERN_INFO "DTLB dump:\n");
  230. dump_dtlb();
  231. die("Bus Error", regs, SIGKILL);
  232. }