fault.c 7.4 KB

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