fault.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * arch/ppc/mm/fault.c
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Derived from "arch/i386/mm/fault.c"
  8. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  9. *
  10. * Modified by Cort Dougan and Paul Mackerras.
  11. *
  12. * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/config.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/mman.h>
  27. #include <linux/mm.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/module.h>
  31. #include <linux/kprobes.h>
  32. #include <asm/page.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/mmu.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/system.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/kdebug.h>
  39. /*
  40. * Check whether the instruction at regs->nip is a store using
  41. * an update addressing form which will update r1.
  42. */
  43. static int store_updates_sp(struct pt_regs *regs)
  44. {
  45. unsigned int inst;
  46. if (get_user(inst, (unsigned int __user *)regs->nip))
  47. return 0;
  48. /* check for 1 in the rA field */
  49. if (((inst >> 16) & 0x1f) != 1)
  50. return 0;
  51. /* check major opcode */
  52. switch (inst >> 26) {
  53. case 37: /* stwu */
  54. case 39: /* stbu */
  55. case 45: /* sthu */
  56. case 53: /* stfsu */
  57. case 55: /* stfdu */
  58. return 1;
  59. case 62: /* std or stdu */
  60. return (inst & 3) == 1;
  61. case 31:
  62. /* check minor opcode */
  63. switch ((inst >> 1) & 0x3ff) {
  64. case 181: /* stdux */
  65. case 183: /* stwux */
  66. case 247: /* stbux */
  67. case 439: /* sthux */
  68. case 695: /* stfsux */
  69. case 759: /* stfdux */
  70. return 1;
  71. }
  72. }
  73. return 0;
  74. }
  75. /*
  76. * The error_code parameter is
  77. * - DSISR for a non-SLB data access fault,
  78. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  79. * - 0 any SLB fault.
  80. * The return value is 0 if the fault was handled, or the signal
  81. * number if this is a kernel fault that can't be handled here.
  82. */
  83. int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
  84. unsigned long error_code)
  85. {
  86. struct vm_area_struct * vma;
  87. struct mm_struct *mm = current->mm;
  88. siginfo_t info;
  89. unsigned long code = SEGV_MAPERR;
  90. unsigned long is_write = error_code & DSISR_ISSTORE;
  91. unsigned long trap = TRAP(regs);
  92. unsigned long is_exec = trap == 0x400;
  93. BUG_ON((trap == 0x380) || (trap == 0x480));
  94. if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, error_code,
  95. 11, SIGSEGV) == NOTIFY_STOP)
  96. return 0;
  97. if (trap == 0x300) {
  98. if (debugger_fault_handler(regs))
  99. return 0;
  100. }
  101. /* On a kernel SLB miss we can only check for a valid exception entry */
  102. if (!user_mode(regs) && (address >= TASK_SIZE))
  103. return SIGSEGV;
  104. if (error_code & DSISR_DABRMATCH) {
  105. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  106. 11, SIGSEGV) == NOTIFY_STOP)
  107. return 0;
  108. if (debugger_dabr_match(regs))
  109. return 0;
  110. }
  111. if (in_atomic() || mm == NULL) {
  112. if (!user_mode(regs))
  113. return SIGSEGV;
  114. /* in_atomic() in user mode is really bad,
  115. as is current->mm == NULL. */
  116. printk(KERN_EMERG "Page fault in user mode with"
  117. "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  118. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  119. regs->nip, regs->msr);
  120. die("Weird page fault", regs, SIGSEGV);
  121. }
  122. /* When running in the kernel we expect faults to occur only to
  123. * addresses in user space. All other faults represent errors in the
  124. * kernel and should generate an OOPS. Unfortunatly, in the case of an
  125. * erroneous fault occuring in a code path which already holds mmap_sem
  126. * we will deadlock attempting to validate the fault against the
  127. * address space. Luckily the kernel only validly references user
  128. * space from well defined areas of code, which are listed in the
  129. * exceptions table.
  130. *
  131. * As the vast majority of faults will be valid we will only perform
  132. * the source reference check when there is a possibilty of a deadlock.
  133. * Attempt to lock the address space, if we cannot we then validate the
  134. * source. If this is invalid we can skip the address space check,
  135. * thus avoiding the deadlock.
  136. */
  137. if (!down_read_trylock(&mm->mmap_sem)) {
  138. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  139. goto bad_area_nosemaphore;
  140. down_read(&mm->mmap_sem);
  141. }
  142. vma = find_vma(mm, address);
  143. if (!vma)
  144. goto bad_area;
  145. if (vma->vm_start <= address) {
  146. goto good_area;
  147. }
  148. if (!(vma->vm_flags & VM_GROWSDOWN))
  149. goto bad_area;
  150. /*
  151. * N.B. The POWER/Open ABI allows programs to access up to
  152. * 288 bytes below the stack pointer.
  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->gpr[1]
  177. && (!user_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. if (is_exec) {
  185. /* protection fault */
  186. if (error_code & DSISR_PROTFAULT)
  187. goto bad_area;
  188. if (!(vma->vm_flags & VM_EXEC))
  189. goto bad_area;
  190. /* a write */
  191. } else if (is_write) {
  192. if (!(vma->vm_flags & VM_WRITE))
  193. goto bad_area;
  194. /* a read */
  195. } else {
  196. if (!(vma->vm_flags & VM_READ))
  197. goto bad_area;
  198. }
  199. survive:
  200. /*
  201. * If for any reason at all we couldn't handle the fault,
  202. * make sure we exit gracefully rather than endlessly redo
  203. * the fault.
  204. */
  205. switch (handle_mm_fault(mm, vma, address, is_write)) {
  206. case VM_FAULT_MINOR:
  207. current->min_flt++;
  208. break;
  209. case VM_FAULT_MAJOR:
  210. current->maj_flt++;
  211. break;
  212. case VM_FAULT_SIGBUS:
  213. goto do_sigbus;
  214. case VM_FAULT_OOM:
  215. goto out_of_memory;
  216. default:
  217. BUG();
  218. }
  219. up_read(&mm->mmap_sem);
  220. return 0;
  221. bad_area:
  222. up_read(&mm->mmap_sem);
  223. bad_area_nosemaphore:
  224. /* User mode accesses cause a SIGSEGV */
  225. if (user_mode(regs)) {
  226. info.si_signo = SIGSEGV;
  227. info.si_errno = 0;
  228. info.si_code = code;
  229. info.si_addr = (void __user *) address;
  230. force_sig_info(SIGSEGV, &info, current);
  231. return 0;
  232. }
  233. if (trap == 0x400 && (error_code & DSISR_PROTFAULT)
  234. && printk_ratelimit())
  235. printk(KERN_CRIT "kernel tried to execute NX-protected"
  236. " page (%lx) - exploit attempt? (uid: %d)\n",
  237. address, current->uid);
  238. return SIGSEGV;
  239. /*
  240. * We ran out of memory, or some other thing happened to us that made
  241. * us unable to handle the page fault gracefully.
  242. */
  243. out_of_memory:
  244. up_read(&mm->mmap_sem);
  245. if (current->pid == 1) {
  246. yield();
  247. down_read(&mm->mmap_sem);
  248. goto survive;
  249. }
  250. printk("VM: killing process %s\n", current->comm);
  251. if (user_mode(regs))
  252. do_exit(SIGKILL);
  253. return SIGKILL;
  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 0;
  263. }
  264. return SIGBUS;
  265. }
  266. /*
  267. * bad_page_fault is called when we have a bad access from the kernel.
  268. * It is called from do_page_fault above and from some of the procedures
  269. * in traps.c.
  270. */
  271. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  272. {
  273. const struct exception_table_entry *entry;
  274. /* Are we prepared to handle this fault? */
  275. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  276. regs->nip = entry->fixup;
  277. return;
  278. }
  279. /* kernel has accessed a bad area */
  280. die("Kernel access of bad area", regs, sig);
  281. }