fault.c 8.6 KB

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