fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. int writeaccess;
  58. long signr;
  59. int code;
  60. int fault;
  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. down_read(&mm->mmap_sem);
  76. vma = find_vma(mm, address);
  77. if (!vma)
  78. goto bad_area;
  79. if (vma->vm_start <= address)
  80. goto good_area;
  81. if (!(vma->vm_flags & VM_GROWSDOWN))
  82. goto bad_area;
  83. if (expand_stack(vma, address))
  84. goto bad_area;
  85. /*
  86. * Ok, we have a good vm_area for this memory access, so we
  87. * can handle it...
  88. */
  89. good_area:
  90. code = SEGV_ACCERR;
  91. writeaccess = 0;
  92. switch (ecr) {
  93. case ECR_PROTECTION_X:
  94. case ECR_TLB_MISS_X:
  95. if (!(vma->vm_flags & VM_EXEC))
  96. goto bad_area;
  97. break;
  98. case ECR_PROTECTION_R:
  99. case ECR_TLB_MISS_R:
  100. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  101. goto bad_area;
  102. break;
  103. case ECR_PROTECTION_W:
  104. case ECR_TLB_MISS_W:
  105. if (!(vma->vm_flags & VM_WRITE))
  106. goto bad_area;
  107. writeaccess = 1;
  108. break;
  109. default:
  110. panic("Unhandled case %lu in do_page_fault!", ecr);
  111. }
  112. /*
  113. * If for any reason at all we couldn't handle the fault, make
  114. * sure we exit gracefully rather than endlessly redo the
  115. * fault.
  116. */
  117. fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
  118. if (unlikely(fault & VM_FAULT_ERROR)) {
  119. if (fault & VM_FAULT_OOM)
  120. goto out_of_memory;
  121. else if (fault & VM_FAULT_SIGSEGV)
  122. goto bad_area;
  123. else if (fault & VM_FAULT_SIGBUS)
  124. goto do_sigbus;
  125. BUG();
  126. }
  127. if (fault & VM_FAULT_MAJOR)
  128. tsk->maj_flt++;
  129. else
  130. tsk->min_flt++;
  131. up_read(&mm->mmap_sem);
  132. return;
  133. /*
  134. * Something tried to access memory that isn't in our memory
  135. * map. Fix it, but check if it's kernel or user first...
  136. */
  137. bad_area:
  138. up_read(&mm->mmap_sem);
  139. if (user_mode(regs)) {
  140. if (exception_trace && printk_ratelimit())
  141. printk("%s%s[%d]: segfault at %08lx pc %08lx "
  142. "sp %08lx ecr %lu\n",
  143. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  144. tsk->comm, tsk->pid, address, regs->pc,
  145. regs->sp, ecr);
  146. _exception(SIGSEGV, regs, code, address);
  147. return;
  148. }
  149. no_context:
  150. /* Are we prepared to handle this kernel fault? */
  151. fixup = search_exception_tables(regs->pc);
  152. if (fixup) {
  153. regs->pc = fixup->fixup;
  154. return;
  155. }
  156. /*
  157. * Oops. The kernel tried to access some bad page. We'll have
  158. * to terminate things with extreme prejudice.
  159. */
  160. if (address < PAGE_SIZE)
  161. printk(KERN_ALERT
  162. "Unable to handle kernel NULL pointer dereference");
  163. else
  164. printk(KERN_ALERT
  165. "Unable to handle kernel paging request");
  166. printk(" at virtual address %08lx\n", address);
  167. page = sysreg_read(PTBR);
  168. printk(KERN_ALERT "ptbr = %08lx", page);
  169. if (address >= TASK_SIZE)
  170. page = (unsigned long)swapper_pg_dir;
  171. if (page) {
  172. page = ((unsigned long *)page)[address >> 22];
  173. printk(" pgd = %08lx", page);
  174. if (page & _PAGE_PRESENT) {
  175. page &= PAGE_MASK;
  176. address &= 0x003ff000;
  177. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  178. printk(" pte = %08lx", page);
  179. }
  180. }
  181. printk("\n");
  182. die("Kernel access of bad area", regs, signr);
  183. return;
  184. /*
  185. * We ran out of memory, or some other thing happened to us
  186. * that made us unable to handle the page fault gracefully.
  187. */
  188. out_of_memory:
  189. up_read(&mm->mmap_sem);
  190. pagefault_out_of_memory();
  191. if (!user_mode(regs))
  192. goto no_context;
  193. return;
  194. do_sigbus:
  195. up_read(&mm->mmap_sem);
  196. /* Kernel mode? Handle exceptions or die */
  197. signr = SIGBUS;
  198. code = BUS_ADRERR;
  199. if (!user_mode(regs))
  200. goto no_context;
  201. if (exception_trace)
  202. printk("%s%s[%d]: bus error at %08lx pc %08lx "
  203. "sp %08lx ecr %lu\n",
  204. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  205. tsk->comm, tsk->pid, address, regs->pc,
  206. regs->sp, ecr);
  207. _exception(SIGBUS, regs, BUS_ADRERR, address);
  208. }
  209. asmlinkage void do_bus_error(unsigned long addr, int write_access,
  210. struct pt_regs *regs)
  211. {
  212. printk(KERN_ALERT
  213. "Bus error at physical address 0x%08lx (%s access)\n",
  214. addr, write_access ? "write" : "read");
  215. printk(KERN_INFO "DTLB dump:\n");
  216. dump_dtlb();
  217. die("Bus Error", regs, SIGKILL);
  218. }