fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Derived from "arch/i386/mm/fault.c"
  6. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  7. *
  8. * Modified by Cort Dougan and Paul Mackerras.
  9. *
  10. * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/mman.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/highmem.h>
  28. #include <linux/module.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kdebug.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/magic.h>
  33. #include <linux/ratelimit.h>
  34. #include <asm/firmware.h>
  35. #include <asm/page.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/mmu.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/system.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/siginfo.h>
  43. #include <mm/mmu_decl.h>
  44. #ifdef CONFIG_KPROBES
  45. static inline int notify_page_fault(struct pt_regs *regs)
  46. {
  47. int ret = 0;
  48. /* kprobe_running() needs smp_processor_id() */
  49. if (!user_mode(regs)) {
  50. preempt_disable();
  51. if (kprobe_running() && kprobe_fault_handler(regs, 11))
  52. ret = 1;
  53. preempt_enable();
  54. }
  55. return ret;
  56. }
  57. #else
  58. static inline int notify_page_fault(struct pt_regs *regs)
  59. {
  60. return 0;
  61. }
  62. #endif
  63. /*
  64. * Check whether the instruction at regs->nip is a store using
  65. * an update addressing form which will update r1.
  66. */
  67. static int store_updates_sp(struct pt_regs *regs)
  68. {
  69. unsigned int inst;
  70. if (get_user(inst, (unsigned int __user *)regs->nip))
  71. return 0;
  72. /* check for 1 in the rA field */
  73. if (((inst >> 16) & 0x1f) != 1)
  74. return 0;
  75. /* check major opcode */
  76. switch (inst >> 26) {
  77. case 37: /* stwu */
  78. case 39: /* stbu */
  79. case 45: /* sthu */
  80. case 53: /* stfsu */
  81. case 55: /* stfdu */
  82. return 1;
  83. case 62: /* std or stdu */
  84. return (inst & 3) == 1;
  85. case 31:
  86. /* check minor opcode */
  87. switch ((inst >> 1) & 0x3ff) {
  88. case 181: /* stdux */
  89. case 183: /* stwux */
  90. case 247: /* stbux */
  91. case 439: /* sthux */
  92. case 695: /* stfsux */
  93. case 759: /* stfdux */
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /*
  100. * For 600- and 800-family processors, the error_code parameter is DSISR
  101. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  102. * the error_code parameter is ESR for a data fault, 0 for an instruction
  103. * fault.
  104. * For 64-bit processors, the error_code parameter is
  105. * - DSISR for a non-SLB data access fault,
  106. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  107. * - 0 any SLB fault.
  108. *
  109. * The return value is 0 if the fault was handled, or the signal
  110. * number if this is a kernel fault that can't be handled here.
  111. */
  112. int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
  113. unsigned long error_code)
  114. {
  115. struct vm_area_struct * vma;
  116. struct mm_struct *mm = current->mm;
  117. siginfo_t info;
  118. int code = SEGV_MAPERR;
  119. int is_write = 0, ret;
  120. int trap = TRAP(regs);
  121. int is_exec = trap == 0x400;
  122. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  123. /*
  124. * Fortunately the bit assignments in SRR1 for an instruction
  125. * fault and DSISR for a data fault are mostly the same for the
  126. * bits we are interested in. But there are some bits which
  127. * indicate errors in DSISR but can validly be set in SRR1.
  128. */
  129. if (trap == 0x400)
  130. error_code &= 0x48200000;
  131. else
  132. is_write = error_code & DSISR_ISSTORE;
  133. #else
  134. is_write = error_code & ESR_DST;
  135. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  136. if (notify_page_fault(regs))
  137. return 0;
  138. if (unlikely(debugger_fault_handler(regs)))
  139. return 0;
  140. /* On a kernel SLB miss we can only check for a valid exception entry */
  141. if (!user_mode(regs) && (address >= TASK_SIZE))
  142. return SIGSEGV;
  143. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \
  144. defined(CONFIG_PPC_BOOK3S_64))
  145. if (error_code & DSISR_DABRMATCH) {
  146. /* DABR match */
  147. do_dabr(regs, address, error_code);
  148. return 0;
  149. }
  150. #endif
  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. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  163. /* When running in the kernel we expect faults to occur only to
  164. * addresses in user space. All other faults represent errors in the
  165. * kernel and should generate an OOPS. Unfortunately, in the case of an
  166. * erroneous fault occurring in a code path which already holds mmap_sem
  167. * we will deadlock attempting to validate the fault against the
  168. * address space. Luckily the kernel only validly references user
  169. * space from well defined areas of code, which are listed in the
  170. * exceptions table.
  171. *
  172. * As the vast majority of faults will be valid we will only perform
  173. * the source reference check when there is a possibility of a deadlock.
  174. * Attempt to lock the address space, if we cannot we then validate the
  175. * source. If this is invalid we can skip the address space check,
  176. * thus avoiding the deadlock.
  177. */
  178. if (!down_read_trylock(&mm->mmap_sem)) {
  179. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  180. goto bad_area_nosemaphore;
  181. down_read(&mm->mmap_sem);
  182. }
  183. vma = find_vma(mm, address);
  184. if (!vma)
  185. goto bad_area;
  186. if (vma->vm_start <= address)
  187. goto good_area;
  188. if (!(vma->vm_flags & VM_GROWSDOWN))
  189. goto bad_area;
  190. /*
  191. * N.B. The POWER/Open ABI allows programs to access up to
  192. * 288 bytes below the stack pointer.
  193. * The kernel signal delivery code writes up to about 1.5kB
  194. * below the stack pointer (r1) before decrementing it.
  195. * The exec code can write slightly over 640kB to the stack
  196. * before setting the user r1. Thus we allow the stack to
  197. * expand to 1MB without further checks.
  198. */
  199. if (address + 0x100000 < vma->vm_end) {
  200. /* get user regs even if this fault is in kernel mode */
  201. struct pt_regs *uregs = current->thread.regs;
  202. if (uregs == NULL)
  203. goto bad_area;
  204. /*
  205. * A user-mode access to an address a long way below
  206. * the stack pointer is only valid if the instruction
  207. * is one which would update the stack pointer to the
  208. * address accessed if the instruction completed,
  209. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  210. * (or the byte, halfword, float or double forms).
  211. *
  212. * If we don't check this then any write to the area
  213. * between the last mapped region and the stack will
  214. * expand the stack rather than segfaulting.
  215. */
  216. if (address + 2048 < uregs->gpr[1]
  217. && (!user_mode(regs) || !store_updates_sp(regs)))
  218. goto bad_area;
  219. }
  220. if (expand_stack(vma, address))
  221. goto bad_area;
  222. good_area:
  223. code = SEGV_ACCERR;
  224. #if defined(CONFIG_6xx)
  225. if (error_code & 0x95700000)
  226. /* an error such as lwarx to I/O controller space,
  227. address matching DABR, eciwx, etc. */
  228. goto bad_area;
  229. #endif /* CONFIG_6xx */
  230. #if defined(CONFIG_8xx)
  231. /* 8xx sometimes need to load a invalid/non-present TLBs.
  232. * These must be invalidated separately as linux mm don't.
  233. */
  234. if (error_code & 0x40000000) /* no translation? */
  235. _tlbil_va(address, 0, 0, 0);
  236. /* The MPC8xx seems to always set 0x80000000, which is
  237. * "undefined". Of those that can be set, this is the only
  238. * one which seems bad.
  239. */
  240. if (error_code & 0x10000000)
  241. /* Guarded storage error. */
  242. goto bad_area;
  243. #endif /* CONFIG_8xx */
  244. if (is_exec) {
  245. #ifdef CONFIG_PPC_STD_MMU
  246. /* Protection fault on exec go straight to failure on
  247. * Hash based MMUs as they either don't support per-page
  248. * execute permission, or if they do, it's handled already
  249. * at the hash level. This test would probably have to
  250. * be removed if we change the way this works to make hash
  251. * processors use the same I/D cache coherency mechanism
  252. * as embedded.
  253. */
  254. if (error_code & DSISR_PROTFAULT)
  255. goto bad_area;
  256. #endif /* CONFIG_PPC_STD_MMU */
  257. /*
  258. * Allow execution from readable areas if the MMU does not
  259. * provide separate controls over reading and executing.
  260. *
  261. * Note: That code used to not be enabled for 4xx/BookE.
  262. * It is now as I/D cache coherency for these is done at
  263. * set_pte_at() time and I see no reason why the test
  264. * below wouldn't be valid on those processors. This -may-
  265. * break programs compiled with a really old ABI though.
  266. */
  267. if (!(vma->vm_flags & VM_EXEC) &&
  268. (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
  269. !(vma->vm_flags & (VM_READ | VM_WRITE))))
  270. goto bad_area;
  271. /* a write */
  272. } else if (is_write) {
  273. if (!(vma->vm_flags & VM_WRITE))
  274. goto bad_area;
  275. /* a read */
  276. } else {
  277. /* protection fault */
  278. if (error_code & 0x08000000)
  279. goto bad_area;
  280. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  281. goto bad_area;
  282. }
  283. /*
  284. * If for any reason at all we couldn't handle the fault,
  285. * make sure we exit gracefully rather than endlessly redo
  286. * the fault.
  287. */
  288. ret = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
  289. if (unlikely(ret & VM_FAULT_ERROR)) {
  290. if (ret & VM_FAULT_OOM)
  291. goto out_of_memory;
  292. else if (ret & VM_FAULT_SIGBUS)
  293. goto do_sigbus;
  294. BUG();
  295. }
  296. if (ret & VM_FAULT_MAJOR) {
  297. current->maj_flt++;
  298. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  299. regs, address);
  300. #ifdef CONFIG_PPC_SMLPAR
  301. if (firmware_has_feature(FW_FEATURE_CMO)) {
  302. preempt_disable();
  303. get_lppaca()->page_ins += (1 << PAGE_FACTOR);
  304. preempt_enable();
  305. }
  306. #endif
  307. } else {
  308. current->min_flt++;
  309. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  310. regs, address);
  311. }
  312. up_read(&mm->mmap_sem);
  313. return 0;
  314. bad_area:
  315. up_read(&mm->mmap_sem);
  316. bad_area_nosemaphore:
  317. /* User mode accesses cause a SIGSEGV */
  318. if (user_mode(regs)) {
  319. _exception(SIGSEGV, regs, code, address);
  320. return 0;
  321. }
  322. if (is_exec && (error_code & DSISR_PROTFAULT))
  323. printk_ratelimited(KERN_CRIT "kernel tried to execute NX-protected"
  324. " page (%lx) - exploit attempt? (uid: %d)\n",
  325. address, current_uid());
  326. return SIGSEGV;
  327. /*
  328. * We ran out of memory, or some other thing happened to us that made
  329. * us unable to handle the page fault gracefully.
  330. */
  331. out_of_memory:
  332. up_read(&mm->mmap_sem);
  333. if (!user_mode(regs))
  334. return SIGKILL;
  335. pagefault_out_of_memory();
  336. return 0;
  337. do_sigbus:
  338. up_read(&mm->mmap_sem);
  339. if (user_mode(regs)) {
  340. info.si_signo = SIGBUS;
  341. info.si_errno = 0;
  342. info.si_code = BUS_ADRERR;
  343. info.si_addr = (void __user *)address;
  344. force_sig_info(SIGBUS, &info, current);
  345. return 0;
  346. }
  347. return SIGBUS;
  348. }
  349. /*
  350. * bad_page_fault is called when we have a bad access from the kernel.
  351. * It is called from the DSI and ISI handlers in head.S and from some
  352. * of the procedures in traps.c.
  353. */
  354. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  355. {
  356. const struct exception_table_entry *entry;
  357. unsigned long *stackend;
  358. /* Are we prepared to handle this fault? */
  359. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  360. regs->nip = entry->fixup;
  361. return;
  362. }
  363. /* kernel has accessed a bad area */
  364. switch (regs->trap) {
  365. case 0x300:
  366. case 0x380:
  367. printk(KERN_ALERT "Unable to handle kernel paging request for "
  368. "data at address 0x%08lx\n", regs->dar);
  369. break;
  370. case 0x400:
  371. case 0x480:
  372. printk(KERN_ALERT "Unable to handle kernel paging request for "
  373. "instruction fetch\n");
  374. break;
  375. default:
  376. printk(KERN_ALERT "Unable to handle kernel paging request for "
  377. "unknown fault\n");
  378. break;
  379. }
  380. printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
  381. regs->nip);
  382. stackend = end_of_stack(current);
  383. if (current != &init_task && *stackend != STACK_END_MAGIC)
  384. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  385. die("Kernel access of bad area", regs, sig);
  386. }