fault.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/ptrace.h>
  27. #include <linux/mman.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/highmem.h>
  31. #include <linux/module.h>
  32. #include <linux/kprobes.h>
  33. #include <asm/page.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/mmu.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/system.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/kdebug.h>
  41. #include <asm/siginfo.h>
  42. /*
  43. * Check whether the instruction at regs->nip 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 __user *)regs->nip))
  50. return 0;
  51. /* check for 1 in the rA field */
  52. if (((inst >> 16) & 0x1f) != 1)
  53. return 0;
  54. /* check major opcode */
  55. switch (inst >> 26) {
  56. case 37: /* stwu */
  57. case 39: /* stbu */
  58. case 45: /* sthu */
  59. case 53: /* stfsu */
  60. case 55: /* stfdu */
  61. return 1;
  62. case 62: /* std or stdu */
  63. return (inst & 3) == 1;
  64. case 31:
  65. /* check minor opcode */
  66. switch ((inst >> 1) & 0x3ff) {
  67. case 181: /* stdux */
  68. case 183: /* stwux */
  69. case 247: /* stbux */
  70. case 439: /* sthux */
  71. case 695: /* stfsux */
  72. case 759: /* stfdux */
  73. return 1;
  74. }
  75. }
  76. return 0;
  77. }
  78. static void do_dabr(struct pt_regs *regs, unsigned long error_code)
  79. {
  80. siginfo_t info;
  81. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  82. 11, SIGSEGV) == NOTIFY_STOP)
  83. return;
  84. if (debugger_dabr_match(regs))
  85. return;
  86. /* Clear the DABR */
  87. set_dabr(0);
  88. /* Deliver the signal to userspace */
  89. info.si_signo = SIGTRAP;
  90. info.si_errno = 0;
  91. info.si_code = TRAP_HWBKPT;
  92. info.si_addr = (void __user *)regs->nip;
  93. force_sig_info(SIGTRAP, &info, current);
  94. }
  95. /*
  96. * For 600- and 800-family processors, the error_code parameter is DSISR
  97. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  98. * the error_code parameter is ESR for a data fault, 0 for an instruction
  99. * fault.
  100. * For 64-bit processors, the error_code parameter is
  101. * - DSISR for a non-SLB data access fault,
  102. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  103. * - 0 any SLB fault.
  104. *
  105. * The return value is 0 if the fault was handled, or the signal
  106. * number if this is a kernel fault that can't be handled here.
  107. */
  108. int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
  109. unsigned long error_code)
  110. {
  111. struct vm_area_struct * vma;
  112. struct mm_struct *mm = current->mm;
  113. siginfo_t info;
  114. int code = SEGV_MAPERR;
  115. int is_write = 0;
  116. int trap = TRAP(regs);
  117. int is_exec = trap == 0x400;
  118. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  119. /*
  120. * Fortunately the bit assignments in SRR1 for an instruction
  121. * fault and DSISR for a data fault are mostly the same for the
  122. * bits we are interested in. But there are some bits which
  123. * indicate errors in DSISR but can validly be set in SRR1.
  124. */
  125. if (trap == 0x400)
  126. error_code &= 0x48200000;
  127. else
  128. is_write = error_code & DSISR_ISSTORE;
  129. #else
  130. is_write = error_code & ESR_DST;
  131. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  132. if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, error_code,
  133. 11, SIGSEGV) == NOTIFY_STOP)
  134. return 0;
  135. if (trap == 0x300) {
  136. if (debugger_fault_handler(regs))
  137. return 0;
  138. }
  139. /* On a kernel SLB miss we can only check for a valid exception entry */
  140. if (!user_mode(regs) && (address >= TASK_SIZE))
  141. return SIGSEGV;
  142. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  143. if (error_code & DSISR_DABRMATCH) {
  144. /* DABR match */
  145. do_dabr(regs, error_code);
  146. return 0;
  147. }
  148. #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
  149. if (in_atomic() || mm == NULL) {
  150. if (!user_mode(regs))
  151. return SIGSEGV;
  152. /* in_atomic() in user mode is really bad,
  153. as is current->mm == NULL. */
  154. printk(KERN_EMERG "Page fault in user mode with"
  155. "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  156. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  157. regs->nip, regs->msr);
  158. die("Weird page fault", regs, SIGSEGV);
  159. }
  160. /* When running in the kernel we expect faults to occur only to
  161. * addresses in user space. All other faults represent errors in the
  162. * kernel and should generate an OOPS. Unfortunatly, in the case of an
  163. * erroneous fault occuring in a code path which already holds mmap_sem
  164. * we will deadlock attempting to validate the fault against the
  165. * address space. Luckily the kernel only validly references user
  166. * space from well defined areas of code, which are listed in the
  167. * exceptions table.
  168. *
  169. * As the vast majority of faults will be valid we will only perform
  170. * the source reference check when there is a possibilty of a deadlock.
  171. * Attempt to lock the address space, if we cannot we then validate the
  172. * source. If this is invalid we can skip the address space check,
  173. * thus avoiding the deadlock.
  174. */
  175. if (!down_read_trylock(&mm->mmap_sem)) {
  176. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  177. goto bad_area_nosemaphore;
  178. down_read(&mm->mmap_sem);
  179. }
  180. vma = find_vma(mm, address);
  181. if (!vma)
  182. goto bad_area;
  183. if (vma->vm_start <= address)
  184. goto good_area;
  185. if (!(vma->vm_flags & VM_GROWSDOWN))
  186. goto bad_area;
  187. /*
  188. * N.B. The POWER/Open ABI allows programs to access up to
  189. * 288 bytes below the stack pointer.
  190. * The kernel signal delivery code writes up to about 1.5kB
  191. * below the stack pointer (r1) before decrementing it.
  192. * The exec code can write slightly over 640kB to the stack
  193. * before setting the user r1. Thus we allow the stack to
  194. * expand to 1MB without further checks.
  195. */
  196. if (address + 0x100000 < vma->vm_end) {
  197. /* get user regs even if this fault is in kernel mode */
  198. struct pt_regs *uregs = current->thread.regs;
  199. if (uregs == NULL)
  200. goto bad_area;
  201. /*
  202. * A user-mode access to an address a long way below
  203. * the stack pointer is only valid if the instruction
  204. * is one which would update the stack pointer to the
  205. * address accessed if the instruction completed,
  206. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  207. * (or the byte, halfword, float or double forms).
  208. *
  209. * If we don't check this then any write to the area
  210. * between the last mapped region and the stack will
  211. * expand the stack rather than segfaulting.
  212. */
  213. if (address + 2048 < uregs->gpr[1]
  214. && (!user_mode(regs) || !store_updates_sp(regs)))
  215. goto bad_area;
  216. }
  217. if (expand_stack(vma, address))
  218. goto bad_area;
  219. good_area:
  220. code = SEGV_ACCERR;
  221. #if defined(CONFIG_6xx)
  222. if (error_code & 0x95700000)
  223. /* an error such as lwarx to I/O controller space,
  224. address matching DABR, eciwx, etc. */
  225. goto bad_area;
  226. #endif /* CONFIG_6xx */
  227. #if defined(CONFIG_8xx)
  228. /* The MPC8xx seems to always set 0x80000000, which is
  229. * "undefined". Of those that can be set, this is the only
  230. * one which seems bad.
  231. */
  232. if (error_code & 0x10000000)
  233. /* Guarded storage error. */
  234. goto bad_area;
  235. #endif /* CONFIG_8xx */
  236. if (is_exec) {
  237. #ifdef CONFIG_PPC64
  238. /* protection fault */
  239. if (error_code & DSISR_PROTFAULT)
  240. goto bad_area;
  241. if (!(vma->vm_flags & VM_EXEC))
  242. goto bad_area;
  243. #endif
  244. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  245. pte_t *ptep;
  246. /* Since 4xx/Book-E supports per-page execute permission,
  247. * we lazily flush dcache to icache. */
  248. ptep = NULL;
  249. if (get_pteptr(mm, address, &ptep) && pte_present(*ptep)) {
  250. struct page *page = pte_page(*ptep);
  251. if (! test_bit(PG_arch_1, &page->flags)) {
  252. flush_dcache_icache_page(page);
  253. set_bit(PG_arch_1, &page->flags);
  254. }
  255. pte_update(ptep, 0, _PAGE_HWEXEC);
  256. _tlbie(address);
  257. pte_unmap(ptep);
  258. up_read(&mm->mmap_sem);
  259. return 0;
  260. }
  261. if (ptep != NULL)
  262. pte_unmap(ptep);
  263. #endif
  264. /* a write */
  265. } else if (is_write) {
  266. if (!(vma->vm_flags & VM_WRITE))
  267. goto bad_area;
  268. /* a read */
  269. } else {
  270. /* protection fault */
  271. if (error_code & 0x08000000)
  272. goto bad_area;
  273. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  274. goto bad_area;
  275. }
  276. /*
  277. * If for any reason at all we couldn't handle the fault,
  278. * make sure we exit gracefully rather than endlessly redo
  279. * the fault.
  280. */
  281. survive:
  282. switch (handle_mm_fault(mm, vma, address, is_write)) {
  283. case VM_FAULT_MINOR:
  284. current->min_flt++;
  285. break;
  286. case VM_FAULT_MAJOR:
  287. current->maj_flt++;
  288. break;
  289. case VM_FAULT_SIGBUS:
  290. goto do_sigbus;
  291. case VM_FAULT_OOM:
  292. goto out_of_memory;
  293. default:
  294. BUG();
  295. }
  296. up_read(&mm->mmap_sem);
  297. return 0;
  298. bad_area:
  299. up_read(&mm->mmap_sem);
  300. bad_area_nosemaphore:
  301. /* User mode accesses cause a SIGSEGV */
  302. if (user_mode(regs)) {
  303. _exception(SIGSEGV, regs, code, address);
  304. return 0;
  305. }
  306. if (is_exec && (error_code & DSISR_PROTFAULT)
  307. && printk_ratelimit())
  308. printk(KERN_CRIT "kernel tried to execute NX-protected"
  309. " page (%lx) - exploit attempt? (uid: %d)\n",
  310. address, current->uid);
  311. return SIGSEGV;
  312. /*
  313. * We ran out of memory, or some other thing happened to us that made
  314. * us unable to handle the page fault gracefully.
  315. */
  316. out_of_memory:
  317. up_read(&mm->mmap_sem);
  318. if (current->pid == 1) {
  319. yield();
  320. down_read(&mm->mmap_sem);
  321. goto survive;
  322. }
  323. printk("VM: killing process %s\n", current->comm);
  324. if (user_mode(regs))
  325. do_exit(SIGKILL);
  326. return SIGKILL;
  327. do_sigbus:
  328. up_read(&mm->mmap_sem);
  329. if (user_mode(regs)) {
  330. info.si_signo = SIGBUS;
  331. info.si_errno = 0;
  332. info.si_code = BUS_ADRERR;
  333. info.si_addr = (void __user *)address;
  334. force_sig_info(SIGBUS, &info, current);
  335. return 0;
  336. }
  337. return SIGBUS;
  338. }
  339. /*
  340. * bad_page_fault is called when we have a bad access from the kernel.
  341. * It is called from the DSI and ISI handlers in head.S and from some
  342. * of the procedures in traps.c.
  343. */
  344. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  345. {
  346. const struct exception_table_entry *entry;
  347. /* Are we prepared to handle this fault? */
  348. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  349. regs->nip = entry->fixup;
  350. return;
  351. }
  352. /* kernel has accessed a bad area */
  353. die("Kernel access of bad area", regs, sig);
  354. }