fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. survive:
  118. fault = handle_mm_fault(mm, vma, address, writeaccess);
  119. if (unlikely(fault & VM_FAULT_ERROR)) {
  120. if (fault & VM_FAULT_OOM)
  121. goto out_of_memory;
  122. else if (fault & VM_FAULT_SIGBUS)
  123. goto do_sigbus;
  124. BUG();
  125. }
  126. if (fault & VM_FAULT_MAJOR)
  127. tsk->maj_flt++;
  128. else
  129. tsk->min_flt++;
  130. up_read(&mm->mmap_sem);
  131. return;
  132. /*
  133. * Something tried to access memory that isn't in our memory
  134. * map. Fix it, but check if it's kernel or user first...
  135. */
  136. bad_area:
  137. up_read(&mm->mmap_sem);
  138. if (user_mode(regs)) {
  139. if (exception_trace && printk_ratelimit())
  140. printk("%s%s[%d]: segfault at %08lx pc %08lx "
  141. "sp %08lx ecr %lu\n",
  142. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  143. tsk->comm, tsk->pid, address, regs->pc,
  144. regs->sp, ecr);
  145. _exception(SIGSEGV, regs, code, address);
  146. return;
  147. }
  148. no_context:
  149. /* Are we prepared to handle this kernel fault? */
  150. fixup = search_exception_tables(regs->pc);
  151. if (fixup) {
  152. regs->pc = fixup->fixup;
  153. return;
  154. }
  155. /*
  156. * Oops. The kernel tried to access some bad page. We'll have
  157. * to terminate things with extreme prejudice.
  158. */
  159. if (address < PAGE_SIZE)
  160. printk(KERN_ALERT
  161. "Unable to handle kernel NULL pointer dereference");
  162. else
  163. printk(KERN_ALERT
  164. "Unable to handle kernel paging request");
  165. printk(" at virtual address %08lx\n", address);
  166. page = sysreg_read(PTBR);
  167. printk(KERN_ALERT "ptbr = %08lx", page);
  168. if (address >= TASK_SIZE)
  169. page = (unsigned long)swapper_pg_dir;
  170. if (page) {
  171. page = ((unsigned long *)page)[address >> 22];
  172. printk(" pgd = %08lx", page);
  173. if (page & _PAGE_PRESENT) {
  174. page &= PAGE_MASK;
  175. address &= 0x003ff000;
  176. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  177. printk(" pte = %08lx", page);
  178. }
  179. }
  180. printk("\n");
  181. die("Kernel access of bad area", regs, signr);
  182. return;
  183. /*
  184. * We ran out of memory, or some other thing happened to us
  185. * that made us unable to handle the page fault gracefully.
  186. */
  187. out_of_memory:
  188. up_read(&mm->mmap_sem);
  189. if (is_global_init(current)) {
  190. yield();
  191. down_read(&mm->mmap_sem);
  192. goto survive;
  193. }
  194. printk("VM: Killing process %s\n", tsk->comm);
  195. if (user_mode(regs))
  196. do_group_exit(SIGKILL);
  197. goto no_context;
  198. do_sigbus:
  199. up_read(&mm->mmap_sem);
  200. /* Kernel mode? Handle exceptions or die */
  201. signr = SIGBUS;
  202. code = BUS_ADRERR;
  203. if (!user_mode(regs))
  204. goto no_context;
  205. if (exception_trace)
  206. printk("%s%s[%d]: bus error at %08lx pc %08lx "
  207. "sp %08lx ecr %lu\n",
  208. is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
  209. tsk->comm, tsk->pid, address, regs->pc,
  210. regs->sp, ecr);
  211. _exception(SIGBUS, regs, BUS_ADRERR, address);
  212. }
  213. asmlinkage void do_bus_error(unsigned long addr, int write_access,
  214. struct pt_regs *regs)
  215. {
  216. printk(KERN_ALERT
  217. "Bus error at physical address 0x%08lx (%s access)\n",
  218. addr, write_access ? "write" : "read");
  219. printk(KERN_INFO "DTLB dump:\n");
  220. dump_dtlb();
  221. die("Bus Error", regs, SIGKILL);
  222. }