fault.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * arch/microblaze/mm/fault.c
  3. *
  4. * Copyright (C) 2007 Xilinx, Inc. All rights reserved.
  5. *
  6. * Derived from "arch/ppc/mm/fault.c"
  7. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  8. *
  9. * Derived from "arch/i386/mm/fault.c"
  10. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  11. *
  12. * Modified by Cort Dougan and Paul Mackerras.
  13. *
  14. * This file is subject to the terms and conditions of the GNU General
  15. * Public License. See the file COPYING in the main directory of this
  16. * archive for more details.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/signal.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/types.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/mman.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/mmu.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/system.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/exceptions.h>
  37. #if defined(CONFIG_KGDB)
  38. int debugger_kernel_faults = 1;
  39. #endif
  40. static unsigned long pte_misses; /* updated by do_page_fault() */
  41. static unsigned long pte_errors; /* updated by do_page_fault() */
  42. /*
  43. * Check whether the instruction at regs->pc is a store using
  44. * an update addressing form which will update r1.
  45. */
  46. static int store_updates_sp(struct pt_regs *regs)
  47. {
  48. unsigned int inst;
  49. if (get_user(inst, (unsigned int *)regs->pc))
  50. return 0;
  51. /* check for 1 in the rD field */
  52. if (((inst >> 21) & 0x1f) != 1)
  53. return 0;
  54. /* check for store opcodes */
  55. if ((inst & 0xd0000000) == 0xd0000000)
  56. return 1;
  57. return 0;
  58. }
  59. /*
  60. * bad_page_fault is called when we have a bad access from the kernel.
  61. * It is called from do_page_fault above and from some of the procedures
  62. * in traps.c.
  63. */
  64. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  65. {
  66. const struct exception_table_entry *fixup;
  67. /* MS: no context */
  68. /* Are we prepared to handle this fault? */
  69. fixup = search_exception_tables(regs->pc);
  70. if (fixup) {
  71. regs->pc = fixup->fixup;
  72. return;
  73. }
  74. /* kernel has accessed a bad area */
  75. #if defined(CONFIG_KGDB)
  76. if (debugger_kernel_faults)
  77. debugger(regs);
  78. #endif
  79. die("kernel access of bad area", regs, sig);
  80. }
  81. /*
  82. * The error_code parameter is ESR for a data fault,
  83. * 0 for an instruction fault.
  84. */
  85. void do_page_fault(struct pt_regs *regs, unsigned long address,
  86. unsigned long error_code)
  87. {
  88. struct vm_area_struct *vma;
  89. struct mm_struct *mm = current->mm;
  90. siginfo_t info;
  91. int code = SEGV_MAPERR;
  92. int is_write = error_code & ESR_S;
  93. int fault;
  94. regs->ear = address;
  95. regs->esr = error_code;
  96. /* On a kernel SLB miss we can only check for a valid exception entry */
  97. if (kernel_mode(regs) && (address >= TASK_SIZE)) {
  98. printk(KERN_WARNING "kernel task_size exceed");
  99. _exception(SIGSEGV, regs, code, address);
  100. }
  101. /* for instr TLB miss and instr storage exception ESR_S is undefined */
  102. if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
  103. is_write = 0;
  104. #if defined(CONFIG_KGDB)
  105. if (debugger_fault_handler && regs->trap == 0x300) {
  106. debugger_fault_handler(regs);
  107. return;
  108. }
  109. #endif /* CONFIG_KGDB */
  110. if (in_atomic() || !mm) {
  111. if (kernel_mode(regs))
  112. goto bad_area_nosemaphore;
  113. /* in_atomic() in user mode is really bad,
  114. as is current->mm == NULL. */
  115. printk(KERN_EMERG "Page fault in user mode with "
  116. "in_atomic(), mm = %p\n", mm);
  117. printk(KERN_EMERG "r15 = %lx MSR = %lx\n",
  118. regs->r15, regs->msr);
  119. die("Weird page fault", regs, SIGSEGV);
  120. }
  121. /* When running in the kernel we expect faults to occur only to
  122. * addresses in user space. All other faults represent errors in the
  123. * kernel and should generate an OOPS. Unfortunately, in the case of an
  124. * erroneous fault occurring in a code path which already holds mmap_sem
  125. * we will deadlock attempting to validate the fault against the
  126. * address space. Luckily the kernel only validly references user
  127. * space from well defined areas of code, which are listed in the
  128. * exceptions table.
  129. *
  130. * As the vast majority of faults will be valid we will only perform
  131. * the source reference check when there is a possibility of a deadlock.
  132. * Attempt to lock the address space, if we cannot we then validate the
  133. * source. If this is invalid we can skip the address space check,
  134. * thus avoiding the deadlock.
  135. */
  136. if (!down_read_trylock(&mm->mmap_sem)) {
  137. if (kernel_mode(regs) && !search_exception_tables(regs->pc))
  138. goto bad_area_nosemaphore;
  139. down_read(&mm->mmap_sem);
  140. }
  141. vma = find_vma(mm, address);
  142. if (!vma)
  143. goto bad_area;
  144. if (vma->vm_start <= address)
  145. goto good_area;
  146. if (!(vma->vm_flags & VM_GROWSDOWN))
  147. goto bad_area;
  148. if (!is_write)
  149. goto bad_area;
  150. /*
  151. * N.B. The ABI allows programs to access up to
  152. * a few hundred bytes below the stack pointer (TBD).
  153. * The kernel signal delivery code writes up to about 1.5kB
  154. * below the stack pointer (r1) before decrementing it.
  155. * The exec code can write slightly over 640kB to the stack
  156. * before setting the user r1. Thus we allow the stack to
  157. * expand to 1MB without further checks.
  158. */
  159. if (address + 0x100000 < vma->vm_end) {
  160. /* get user regs even if this fault is in kernel mode */
  161. struct pt_regs *uregs = current->thread.regs;
  162. if (uregs == NULL)
  163. goto bad_area;
  164. /*
  165. * A user-mode access to an address a long way below
  166. * the stack pointer is only valid if the instruction
  167. * is one which would update the stack pointer to the
  168. * address accessed if the instruction completed,
  169. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  170. * (or the byte, halfword, float or double forms).
  171. *
  172. * If we don't check this then any write to the area
  173. * between the last mapped region and the stack will
  174. * expand the stack rather than segfaulting.
  175. */
  176. if (address + 2048 < uregs->r1
  177. && (kernel_mode(regs) || !store_updates_sp(regs)))
  178. goto bad_area;
  179. }
  180. if (expand_stack(vma, address))
  181. goto bad_area;
  182. good_area:
  183. code = SEGV_ACCERR;
  184. /* a write */
  185. if (is_write) {
  186. if (!(vma->vm_flags & VM_WRITE))
  187. goto bad_area;
  188. /* a read */
  189. } else {
  190. /* protection fault */
  191. if (error_code & 0x08000000)
  192. goto bad_area;
  193. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  194. goto bad_area;
  195. }
  196. /*
  197. * If for any reason at all we couldn't handle the fault,
  198. * make sure we exit gracefully rather than endlessly redo
  199. * the fault.
  200. */
  201. survive:
  202. fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
  203. if (unlikely(fault & VM_FAULT_ERROR)) {
  204. if (fault & VM_FAULT_OOM)
  205. goto out_of_memory;
  206. else if (fault & VM_FAULT_SIGBUS)
  207. goto do_sigbus;
  208. BUG();
  209. }
  210. if (fault & VM_FAULT_MAJOR)
  211. current->maj_flt++;
  212. else
  213. current->min_flt++;
  214. up_read(&mm->mmap_sem);
  215. /*
  216. * keep track of tlb+htab misses that are good addrs but
  217. * just need pte's created via handle_mm_fault()
  218. * -- Cort
  219. */
  220. pte_misses++;
  221. return;
  222. bad_area:
  223. up_read(&mm->mmap_sem);
  224. bad_area_nosemaphore:
  225. pte_errors++;
  226. /* User mode accesses cause a SIGSEGV */
  227. if (user_mode(regs)) {
  228. _exception(SIGSEGV, regs, code, address);
  229. /* info.si_signo = SIGSEGV;
  230. info.si_errno = 0;
  231. info.si_code = code;
  232. info.si_addr = (void *) address;
  233. force_sig_info(SIGSEGV, &info, current);*/
  234. return;
  235. }
  236. bad_page_fault(regs, address, SIGSEGV);
  237. return;
  238. /*
  239. * We ran out of memory, or some other thing happened to us that made
  240. * us unable to handle the page fault gracefully.
  241. */
  242. out_of_memory:
  243. if (current->pid == 1) {
  244. yield();
  245. down_read(&mm->mmap_sem);
  246. goto survive;
  247. }
  248. up_read(&mm->mmap_sem);
  249. printk(KERN_WARNING "VM: killing process %s\n", current->comm);
  250. if (user_mode(regs))
  251. do_exit(SIGKILL);
  252. bad_page_fault(regs, address, SIGKILL);
  253. return;
  254. do_sigbus:
  255. up_read(&mm->mmap_sem);
  256. if (user_mode(regs)) {
  257. info.si_signo = SIGBUS;
  258. info.si_errno = 0;
  259. info.si_code = BUS_ADRERR;
  260. info.si_addr = (void __user *)address;
  261. force_sig_info(SIGBUS, &info, current);
  262. return;
  263. }
  264. bad_page_fault(regs, address, SIGBUS);
  265. }