fault.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/mmu.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/kdebug.h>
  38. /*
  39. * Check whether the instruction at regs->nip is a store using
  40. * an update addressing form which will update r1.
  41. */
  42. static int store_updates_sp(struct pt_regs *regs)
  43. {
  44. unsigned int inst;
  45. if (get_user(inst, (unsigned int __user *)regs->nip))
  46. return 0;
  47. /* check for 1 in the rA field */
  48. if (((inst >> 16) & 0x1f) != 1)
  49. return 0;
  50. /* check major opcode */
  51. switch (inst >> 26) {
  52. case 37: /* stwu */
  53. case 39: /* stbu */
  54. case 45: /* sthu */
  55. case 53: /* stfsu */
  56. case 55: /* stfdu */
  57. return 1;
  58. case 62: /* std or stdu */
  59. return (inst & 3) == 1;
  60. case 31:
  61. /* check minor opcode */
  62. switch ((inst >> 1) & 0x3ff) {
  63. case 181: /* stdux */
  64. case 183: /* stwux */
  65. case 247: /* stbux */
  66. case 439: /* sthux */
  67. case 695: /* stfsux */
  68. case 759: /* stfdux */
  69. return 1;
  70. }
  71. }
  72. return 0;
  73. }
  74. /*
  75. * The error_code parameter is
  76. * - DSISR for a non-SLB data access fault,
  77. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  78. * - 0 any SLB fault.
  79. * The return value is 0 if the fault was handled, or the signal
  80. * number if this is a kernel fault that can't be handled here.
  81. */
  82. int do_page_fault(struct pt_regs *regs, unsigned long address,
  83. unsigned long error_code)
  84. {
  85. struct vm_area_struct * vma;
  86. struct mm_struct *mm = current->mm;
  87. siginfo_t info;
  88. unsigned long code = SEGV_MAPERR;
  89. unsigned long is_write = error_code & DSISR_ISSTORE;
  90. unsigned long trap = TRAP(regs);
  91. unsigned long is_exec = trap == 0x400;
  92. BUG_ON((trap == 0x380) || (trap == 0x480));
  93. if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, error_code,
  94. 11, SIGSEGV) == NOTIFY_STOP)
  95. return 0;
  96. if (trap == 0x300) {
  97. if (debugger_fault_handler(regs))
  98. return 0;
  99. }
  100. /* On a kernel SLB miss we can only check for a valid exception entry */
  101. if (!user_mode(regs) && (address >= TASK_SIZE))
  102. return SIGSEGV;
  103. if (error_code & DSISR_DABRMATCH) {
  104. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  105. 11, SIGSEGV) == NOTIFY_STOP)
  106. return 0;
  107. if (debugger_dabr_match(regs))
  108. return 0;
  109. }
  110. if (in_atomic() || mm == NULL) {
  111. if (!user_mode(regs))
  112. return SIGSEGV;
  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() = %d mm = %p\n", in_atomic(), mm);
  117. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  118. regs->nip, 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. Unfortunatly, in the case of an
  124. * erroneous fault occuring 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 possibilty 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 (!user_mode(regs) && !search_exception_tables(regs->nip))
  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. }
  147. if (!(vma->vm_flags & VM_GROWSDOWN))
  148. goto bad_area;
  149. /*
  150. * N.B. The POWER/Open ABI allows programs to access up to
  151. * 288 bytes below the stack pointer.
  152. * The kernel signal delivery code writes up to about 1.5kB
  153. * below the stack pointer (r1) before decrementing it.
  154. * The exec code can write slightly over 640kB to the stack
  155. * before setting the user r1. Thus we allow the stack to
  156. * expand to 1MB without further checks.
  157. */
  158. if (address + 0x100000 < vma->vm_end) {
  159. /* get user regs even if this fault is in kernel mode */
  160. struct pt_regs *uregs = current->thread.regs;
  161. if (uregs == NULL)
  162. goto bad_area;
  163. /*
  164. * A user-mode access to an address a long way below
  165. * the stack pointer is only valid if the instruction
  166. * is one which would update the stack pointer to the
  167. * address accessed if the instruction completed,
  168. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  169. * (or the byte, halfword, float or double forms).
  170. *
  171. * If we don't check this then any write to the area
  172. * between the last mapped region and the stack will
  173. * expand the stack rather than segfaulting.
  174. */
  175. if (address + 2048 < uregs->gpr[1]
  176. && (!user_mode(regs) || !store_updates_sp(regs)))
  177. goto bad_area;
  178. }
  179. if (expand_stack(vma, address))
  180. goto bad_area;
  181. good_area:
  182. code = SEGV_ACCERR;
  183. if (is_exec) {
  184. /* protection fault */
  185. if (error_code & DSISR_PROTFAULT)
  186. goto bad_area;
  187. if (!(vma->vm_flags & VM_EXEC))
  188. goto bad_area;
  189. /* a write */
  190. } else if (is_write) {
  191. if (!(vma->vm_flags & VM_WRITE))
  192. goto bad_area;
  193. /* a read */
  194. } else {
  195. if (!(vma->vm_flags & VM_READ))
  196. goto bad_area;
  197. }
  198. survive:
  199. /*
  200. * If for any reason at all we couldn't handle the fault,
  201. * make sure we exit gracefully rather than endlessly redo
  202. * the fault.
  203. */
  204. switch (handle_mm_fault(mm, vma, address, is_write)) {
  205. case VM_FAULT_MINOR:
  206. current->min_flt++;
  207. break;
  208. case VM_FAULT_MAJOR:
  209. current->maj_flt++;
  210. break;
  211. case VM_FAULT_SIGBUS:
  212. goto do_sigbus;
  213. case VM_FAULT_OOM:
  214. goto out_of_memory;
  215. default:
  216. BUG();
  217. }
  218. up_read(&mm->mmap_sem);
  219. return 0;
  220. bad_area:
  221. up_read(&mm->mmap_sem);
  222. bad_area_nosemaphore:
  223. /* User mode accesses cause a SIGSEGV */
  224. if (user_mode(regs)) {
  225. info.si_signo = SIGSEGV;
  226. info.si_errno = 0;
  227. info.si_code = code;
  228. info.si_addr = (void __user *) address;
  229. force_sig_info(SIGSEGV, &info, current);
  230. return 0;
  231. }
  232. if (trap == 0x400 && (error_code & DSISR_PROTFAULT)
  233. && printk_ratelimit())
  234. printk(KERN_CRIT "kernel tried to execute NX-protected"
  235. " page (%lx) - exploit attempt? (uid: %d)\n",
  236. address, current->uid);
  237. return SIGSEGV;
  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. up_read(&mm->mmap_sem);
  244. if (current->pid == 1) {
  245. yield();
  246. down_read(&mm->mmap_sem);
  247. goto survive;
  248. }
  249. printk("VM: killing process %s\n", current->comm);
  250. if (user_mode(regs))
  251. do_exit(SIGKILL);
  252. return SIGKILL;
  253. do_sigbus:
  254. up_read(&mm->mmap_sem);
  255. if (user_mode(regs)) {
  256. info.si_signo = SIGBUS;
  257. info.si_errno = 0;
  258. info.si_code = BUS_ADRERR;
  259. info.si_addr = (void __user *)address;
  260. force_sig_info(SIGBUS, &info, current);
  261. return 0;
  262. }
  263. return SIGBUS;
  264. }
  265. /*
  266. * bad_page_fault is called when we have a bad access from the kernel.
  267. * It is called from do_page_fault above and from some of the procedures
  268. * in traps.c.
  269. */
  270. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  271. {
  272. const struct exception_table_entry *entry;
  273. /* Are we prepared to handle this fault? */
  274. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  275. regs->nip = entry->fixup;
  276. return;
  277. }
  278. /* kernel has accessed a bad area */
  279. die("Kernel access of bad area", regs, sig);
  280. }