fault.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. static void do_dabr(struct pt_regs *regs, unsigned long error_code)
  76. {
  77. siginfo_t info;
  78. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  79. 11, SIGSEGV) == NOTIFY_STOP)
  80. return;
  81. if (debugger_dabr_match(regs))
  82. return;
  83. /* Clear the DABR */
  84. set_dabr(0);
  85. /* Deliver the signal to userspace */
  86. info.si_signo = SIGTRAP;
  87. info.si_errno = 0;
  88. info.si_code = TRAP_HWBKPT;
  89. info.si_addr = (void __user *)regs->nip;
  90. force_sig_info(SIGTRAP, &info, current);
  91. }
  92. /*
  93. * The error_code parameter is
  94. * - DSISR for a non-SLB data access fault,
  95. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  96. * - 0 any SLB fault.
  97. * The return value is 0 if the fault was handled, or the signal
  98. * number if this is a kernel fault that can't be handled here.
  99. */
  100. int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
  101. unsigned long error_code)
  102. {
  103. struct vm_area_struct * vma;
  104. struct mm_struct *mm = current->mm;
  105. siginfo_t info;
  106. unsigned long code = SEGV_MAPERR;
  107. unsigned long is_write = error_code & DSISR_ISSTORE;
  108. unsigned long trap = TRAP(regs);
  109. unsigned long is_exec = trap == 0x400;
  110. BUG_ON((trap == 0x380) || (trap == 0x480));
  111. if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, error_code,
  112. 11, SIGSEGV) == NOTIFY_STOP)
  113. return 0;
  114. if (trap == 0x300) {
  115. if (debugger_fault_handler(regs))
  116. return 0;
  117. }
  118. /* On a kernel SLB miss we can only check for a valid exception entry */
  119. if (!user_mode(regs) && (address >= TASK_SIZE))
  120. return SIGSEGV;
  121. if (error_code & DSISR_DABRMATCH) {
  122. do_dabr(regs, error_code);
  123. return 0;
  124. }
  125. if (in_atomic() || mm == NULL) {
  126. if (!user_mode(regs))
  127. return SIGSEGV;
  128. /* in_atomic() in user mode is really bad,
  129. as is current->mm == NULL. */
  130. printk(KERN_EMERG "Page fault in user mode with"
  131. "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  132. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  133. regs->nip, regs->msr);
  134. die("Weird page fault", regs, SIGSEGV);
  135. }
  136. /* When running in the kernel we expect faults to occur only to
  137. * addresses in user space. All other faults represent errors in the
  138. * kernel and should generate an OOPS. Unfortunatly, in the case of an
  139. * erroneous fault occuring in a code path which already holds mmap_sem
  140. * we will deadlock attempting to validate the fault against the
  141. * address space. Luckily the kernel only validly references user
  142. * space from well defined areas of code, which are listed in the
  143. * exceptions table.
  144. *
  145. * As the vast majority of faults will be valid we will only perform
  146. * the source reference check when there is a possibilty of a deadlock.
  147. * Attempt to lock the address space, if we cannot we then validate the
  148. * source. If this is invalid we can skip the address space check,
  149. * thus avoiding the deadlock.
  150. */
  151. if (!down_read_trylock(&mm->mmap_sem)) {
  152. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  153. goto bad_area_nosemaphore;
  154. down_read(&mm->mmap_sem);
  155. }
  156. vma = find_vma(mm, address);
  157. if (!vma)
  158. goto bad_area;
  159. if (vma->vm_start <= address) {
  160. goto good_area;
  161. }
  162. if (!(vma->vm_flags & VM_GROWSDOWN))
  163. goto bad_area;
  164. /*
  165. * N.B. The POWER/Open ABI allows programs to access up to
  166. * 288 bytes below the stack pointer.
  167. * The kernel signal delivery code writes up to about 1.5kB
  168. * below the stack pointer (r1) before decrementing it.
  169. * The exec code can write slightly over 640kB to the stack
  170. * before setting the user r1. Thus we allow the stack to
  171. * expand to 1MB without further checks.
  172. */
  173. if (address + 0x100000 < vma->vm_end) {
  174. /* get user regs even if this fault is in kernel mode */
  175. struct pt_regs *uregs = current->thread.regs;
  176. if (uregs == NULL)
  177. goto bad_area;
  178. /*
  179. * A user-mode access to an address a long way below
  180. * the stack pointer is only valid if the instruction
  181. * is one which would update the stack pointer to the
  182. * address accessed if the instruction completed,
  183. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  184. * (or the byte, halfword, float or double forms).
  185. *
  186. * If we don't check this then any write to the area
  187. * between the last mapped region and the stack will
  188. * expand the stack rather than segfaulting.
  189. */
  190. if (address + 2048 < uregs->gpr[1]
  191. && (!user_mode(regs) || !store_updates_sp(regs)))
  192. goto bad_area;
  193. }
  194. if (expand_stack(vma, address))
  195. goto bad_area;
  196. good_area:
  197. code = SEGV_ACCERR;
  198. if (is_exec) {
  199. /* protection fault */
  200. if (error_code & DSISR_PROTFAULT)
  201. goto bad_area;
  202. if (!(vma->vm_flags & VM_EXEC))
  203. goto bad_area;
  204. /* a write */
  205. } else if (is_write) {
  206. if (!(vma->vm_flags & VM_WRITE))
  207. goto bad_area;
  208. /* a read */
  209. } else {
  210. if (!(vma->vm_flags & VM_READ))
  211. goto bad_area;
  212. }
  213. survive:
  214. /*
  215. * If for any reason at all we couldn't handle the fault,
  216. * make sure we exit gracefully rather than endlessly redo
  217. * the fault.
  218. */
  219. switch (handle_mm_fault(mm, vma, address, is_write)) {
  220. case VM_FAULT_MINOR:
  221. current->min_flt++;
  222. break;
  223. case VM_FAULT_MAJOR:
  224. current->maj_flt++;
  225. break;
  226. case VM_FAULT_SIGBUS:
  227. goto do_sigbus;
  228. case VM_FAULT_OOM:
  229. goto out_of_memory;
  230. default:
  231. BUG();
  232. }
  233. up_read(&mm->mmap_sem);
  234. return 0;
  235. bad_area:
  236. up_read(&mm->mmap_sem);
  237. bad_area_nosemaphore:
  238. /* User mode accesses cause a SIGSEGV */
  239. if (user_mode(regs)) {
  240. info.si_signo = SIGSEGV;
  241. info.si_errno = 0;
  242. info.si_code = code;
  243. info.si_addr = (void __user *) address;
  244. force_sig_info(SIGSEGV, &info, current);
  245. return 0;
  246. }
  247. if (trap == 0x400 && (error_code & DSISR_PROTFAULT)
  248. && printk_ratelimit())
  249. printk(KERN_CRIT "kernel tried to execute NX-protected"
  250. " page (%lx) - exploit attempt? (uid: %d)\n",
  251. address, current->uid);
  252. return SIGSEGV;
  253. /*
  254. * We ran out of memory, or some other thing happened to us that made
  255. * us unable to handle the page fault gracefully.
  256. */
  257. out_of_memory:
  258. up_read(&mm->mmap_sem);
  259. if (current->pid == 1) {
  260. yield();
  261. down_read(&mm->mmap_sem);
  262. goto survive;
  263. }
  264. printk("VM: killing process %s\n", current->comm);
  265. if (user_mode(regs))
  266. do_exit(SIGKILL);
  267. return SIGKILL;
  268. do_sigbus:
  269. up_read(&mm->mmap_sem);
  270. if (user_mode(regs)) {
  271. info.si_signo = SIGBUS;
  272. info.si_errno = 0;
  273. info.si_code = BUS_ADRERR;
  274. info.si_addr = (void __user *)address;
  275. force_sig_info(SIGBUS, &info, current);
  276. return 0;
  277. }
  278. return SIGBUS;
  279. }
  280. /*
  281. * bad_page_fault is called when we have a bad access from the kernel.
  282. * It is called from do_page_fault above and from some of the procedures
  283. * in traps.c.
  284. */
  285. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  286. {
  287. const struct exception_table_entry *entry;
  288. /* Are we prepared to handle this fault? */
  289. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  290. regs->nip = entry->fixup;
  291. return;
  292. }
  293. /* kernel has accessed a bad area */
  294. die("Kernel access of bad area", regs, sig);
  295. }