fault.c 6.3 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/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. if (notify_page_fault(regs, ecr))
  61. return;
  62. address = sysreg_read(TLBEAR);
  63. tsk = current;
  64. mm = tsk->mm;
  65. signr = SIGSEGV;
  66. code = SEGV_MAPERR;
  67. /*
  68. * If we're in an interrupt or have no user context, we must
  69. * not take the fault...
  70. */
  71. if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
  72. goto no_context;
  73. local_irq_enable();
  74. down_read(&mm->mmap_sem);
  75. vma = find_vma(mm, address);
  76. if (!vma)
  77. goto bad_area;
  78. if (vma->vm_start <= address)
  79. goto good_area;
  80. if (!(vma->vm_flags & VM_GROWSDOWN))
  81. goto bad_area;
  82. if (expand_stack(vma, address))
  83. goto bad_area;
  84. /*
  85. * Ok, we have a good vm_area for this memory access, so we
  86. * can handle it...
  87. */
  88. good_area:
  89. code = SEGV_ACCERR;
  90. writeaccess = 0;
  91. switch (ecr) {
  92. case ECR_PROTECTION_X:
  93. case ECR_TLB_MISS_X:
  94. if (!(vma->vm_flags & VM_EXEC))
  95. goto bad_area;
  96. break;
  97. case ECR_PROTECTION_R:
  98. case ECR_TLB_MISS_R:
  99. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  100. goto bad_area;
  101. break;
  102. case ECR_PROTECTION_W:
  103. case ECR_TLB_MISS_W:
  104. if (!(vma->vm_flags & VM_WRITE))
  105. goto bad_area;
  106. writeaccess = 1;
  107. break;
  108. default:
  109. panic("Unhandled case %lu in do_page_fault!", ecr);
  110. }
  111. /*
  112. * If for any reason at all we couldn't handle the fault, make
  113. * sure we exit gracefully rather than endlessly redo the
  114. * fault.
  115. */
  116. survive:
  117. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  118. case VM_FAULT_MINOR:
  119. tsk->min_flt++;
  120. break;
  121. case VM_FAULT_MAJOR:
  122. tsk->maj_flt++;
  123. break;
  124. case VM_FAULT_SIGBUS:
  125. goto do_sigbus;
  126. case VM_FAULT_OOM:
  127. goto out_of_memory;
  128. default:
  129. BUG();
  130. }
  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_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 (page) {
  170. page = ((unsigned long *)page)[address >> 22];
  171. printk(" pgd = %08lx", page);
  172. if (page & _PAGE_PRESENT) {
  173. page &= PAGE_MASK;
  174. address &= 0x003ff000;
  175. page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
  176. printk(" pte = %08lx", page);
  177. }
  178. }
  179. printk("\n");
  180. die("Kernel access of bad area", regs, signr);
  181. return;
  182. /*
  183. * We ran out of memory, or some other thing happened to us
  184. * that made us unable to handle the page fault gracefully.
  185. */
  186. out_of_memory:
  187. up_read(&mm->mmap_sem);
  188. if (is_init(current)) {
  189. yield();
  190. down_read(&mm->mmap_sem);
  191. goto survive;
  192. }
  193. printk("VM: Killing process %s\n", tsk->comm);
  194. if (user_mode(regs))
  195. do_exit(SIGKILL);
  196. goto no_context;
  197. do_sigbus:
  198. up_read(&mm->mmap_sem);
  199. /* Kernel mode? Handle exceptions or die */
  200. signr = SIGBUS;
  201. code = BUS_ADRERR;
  202. if (!user_mode(regs))
  203. goto no_context;
  204. if (exception_trace)
  205. printk("%s%s[%d]: bus error at %08lx pc %08lx "
  206. "sp %08lx ecr %lu\n",
  207. is_init(tsk) ? KERN_EMERG : KERN_INFO,
  208. tsk->comm, tsk->pid, address, regs->pc,
  209. regs->sp, ecr);
  210. _exception(SIGBUS, regs, BUS_ADRERR, address);
  211. }
  212. asmlinkage void do_bus_error(unsigned long addr, int write_access,
  213. struct pt_regs *regs)
  214. {
  215. printk(KERN_ALERT
  216. "Bus error at physical address 0x%08lx (%s access)\n",
  217. addr, write_access ? "write" : "read");
  218. printk(KERN_INFO "DTLB dump:\n");
  219. dump_dtlb();
  220. die("Bus Error", regs, SIGKILL);
  221. }
  222. /*
  223. * This functionality is currently not possible to implement because
  224. * we're using segmentation to ensure a fixed mapping of the kernel
  225. * virtual address space.
  226. *
  227. * It would be possible to implement this, but it would require us to
  228. * disable segmentation at startup and load the kernel mappings into
  229. * the TLB like any other pages. There will be lots of trickery to
  230. * avoid recursive invocation of the TLB miss handler, though...
  231. */
  232. #ifdef CONFIG_DEBUG_PAGEALLOC
  233. void kernel_map_pages(struct page *page, int numpages, int enable)
  234. {
  235. }
  236. EXPORT_SYMBOL(kernel_map_pages);
  237. #endif