fault.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <asm/mmu_context.h>
  16. #include <asm/sysreg.h>
  17. #include <asm/tlb.h>
  18. #include <asm/uaccess.h>
  19. #ifdef CONFIG_KPROBES
  20. ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
  21. /* Hook to register for page fault notifications */
  22. int register_page_fault_notifier(struct notifier_block *nb)
  23. {
  24. return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
  25. }
  26. int unregister_page_fault_notifier(struct notifier_block *nb)
  27. {
  28. return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
  29. }
  30. static inline int notify_page_fault(enum die_val val, struct pt_regs *regs,
  31. int trap, int sig)
  32. {
  33. struct die_args args = {
  34. .regs = regs,
  35. .trapnr = trap,
  36. };
  37. return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
  38. }
  39. #else
  40. static inline int notify_page_fault(enum die_val val, struct pt_regs *regs,
  41. int trap, int sig)
  42. {
  43. return NOTIFY_DONE;
  44. }
  45. #endif
  46. int exception_trace = 1;
  47. /*
  48. * This routine handles page faults. It determines the address and the
  49. * problem, and then passes it off to one of the appropriate routines.
  50. *
  51. * ecr is the Exception Cause Register. Possible values are:
  52. * 6: Protection fault (instruction access)
  53. * 15: Protection fault (read access)
  54. * 16: Protection fault (write access)
  55. * 20: Page not found (instruction access)
  56. * 24: Page not found (read access)
  57. * 28: Page not found (write access)
  58. */
  59. asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
  60. {
  61. struct task_struct *tsk;
  62. struct mm_struct *mm;
  63. struct vm_area_struct *vma;
  64. const struct exception_table_entry *fixup;
  65. unsigned long address;
  66. unsigned long page;
  67. int writeaccess;
  68. long signr;
  69. int code;
  70. if (notify_page_fault(DIE_PAGE_FAULT, regs,
  71. ecr, SIGSEGV) == NOTIFY_STOP)
  72. return;
  73. address = sysreg_read(TLBEAR);
  74. tsk = current;
  75. mm = tsk->mm;
  76. signr = SIGSEGV;
  77. code = SEGV_MAPERR;
  78. /*
  79. * If we're in an interrupt or have no user context, we must
  80. * not take the fault...
  81. */
  82. if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
  83. goto no_context;
  84. local_irq_enable();
  85. down_read(&mm->mmap_sem);
  86. vma = find_vma(mm, address);
  87. if (!vma)
  88. goto bad_area;
  89. if (vma->vm_start <= address)
  90. goto good_area;
  91. if (!(vma->vm_flags & VM_GROWSDOWN))
  92. goto bad_area;
  93. if (expand_stack(vma, address))
  94. goto bad_area;
  95. /*
  96. * Ok, we have a good vm_area for this memory access, so we
  97. * can handle it...
  98. */
  99. good_area:
  100. code = SEGV_ACCERR;
  101. writeaccess = 0;
  102. switch (ecr) {
  103. case ECR_PROTECTION_X:
  104. case ECR_TLB_MISS_X:
  105. if (!(vma->vm_flags & VM_EXEC))
  106. goto bad_area;
  107. break;
  108. case ECR_PROTECTION_R:
  109. case ECR_TLB_MISS_R:
  110. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  111. goto bad_area;
  112. break;
  113. case ECR_PROTECTION_W:
  114. case ECR_TLB_MISS_W:
  115. if (!(vma->vm_flags & VM_WRITE))
  116. goto bad_area;
  117. writeaccess = 1;
  118. break;
  119. default:
  120. panic("Unhandled case %lu in do_page_fault!", ecr);
  121. }
  122. /*
  123. * If for any reason at all we couldn't handle the fault, make
  124. * sure we exit gracefully rather than endlessly redo the
  125. * fault.
  126. */
  127. survive:
  128. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  129. case VM_FAULT_MINOR:
  130. tsk->min_flt++;
  131. break;
  132. case VM_FAULT_MAJOR:
  133. tsk->maj_flt++;
  134. break;
  135. case VM_FAULT_SIGBUS:
  136. goto do_sigbus;
  137. case VM_FAULT_OOM:
  138. goto out_of_memory;
  139. default:
  140. BUG();
  141. }
  142. up_read(&mm->mmap_sem);
  143. return;
  144. /*
  145. * Something tried to access memory that isn't in our memory
  146. * map. Fix it, but check if it's kernel or user first...
  147. */
  148. bad_area:
  149. up_read(&mm->mmap_sem);
  150. if (user_mode(regs)) {
  151. if (exception_trace)
  152. printk("%s%s[%d]: segfault at %08lx pc %08lx "
  153. "sp %08lx ecr %lu\n",
  154. is_init(tsk) ? KERN_EMERG : KERN_INFO,
  155. tsk->comm, tsk->pid, address, regs->pc,
  156. regs->sp, ecr);
  157. _exception(SIGSEGV, regs, code, address);
  158. return;
  159. }
  160. no_context:
  161. /* Are we prepared to handle this kernel fault? */
  162. fixup = search_exception_tables(regs->pc);
  163. if (fixup) {
  164. regs->pc = fixup->fixup;
  165. return;
  166. }
  167. /*
  168. * Oops. The kernel tried to access some bad page. We'll have
  169. * to terminate things with extreme prejudice.
  170. */
  171. if (address < PAGE_SIZE)
  172. printk(KERN_ALERT
  173. "Unable to handle kernel NULL pointer dereference");
  174. else
  175. printk(KERN_ALERT
  176. "Unable to handle kernel paging request");
  177. printk(" at virtual address %08lx\n", address);
  178. page = sysreg_read(PTBR);
  179. printk(KERN_ALERT "ptbr = %08lx", page);
  180. if (page) {
  181. page = ((unsigned long *)page)[address >> 22];
  182. printk(" pgd = %08lx", page);
  183. if (page & _PAGE_PRESENT) {
  184. page &= PAGE_MASK;
  185. address &= 0x003ff000;
  186. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  187. printk(" pte = %08lx", page);
  188. }
  189. }
  190. printk("\n");
  191. die("Kernel access of bad area", regs, signr);
  192. return;
  193. /*
  194. * We ran out of memory, or some other thing happened to us
  195. * that made us unable to handle the page fault gracefully.
  196. */
  197. out_of_memory:
  198. up_read(&mm->mmap_sem);
  199. if (is_init(current)) {
  200. yield();
  201. down_read(&mm->mmap_sem);
  202. goto survive;
  203. }
  204. printk("VM: Killing process %s\n", tsk->comm);
  205. if (user_mode(regs))
  206. do_exit(SIGKILL);
  207. goto no_context;
  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_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. }
  233. /*
  234. * This functionality is currently not possible to implement because
  235. * we're using segmentation to ensure a fixed mapping of the kernel
  236. * virtual address space.
  237. *
  238. * It would be possible to implement this, but it would require us to
  239. * disable segmentation at startup and load the kernel mappings into
  240. * the TLB like any other pages. There will be lots of trickery to
  241. * avoid recursive invocation of the TLB miss handler, though...
  242. */
  243. #ifdef CONFIG_DEBUG_PAGEALLOC
  244. void kernel_map_pages(struct page *page, int numpages, int enable)
  245. {
  246. }
  247. EXPORT_SYMBOL(kernel_map_pages);
  248. #endif