fault.c 12 KB

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