fault.c 11 KB

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