fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 <asm/firmware.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/siginfo.h>
  41. #include <mm/mmu_decl.h>
  42. #ifdef CONFIG_KPROBES
  43. static inline int notify_page_fault(struct pt_regs *regs)
  44. {
  45. int ret = 0;
  46. /* kprobe_running() needs smp_processor_id() */
  47. if (!user_mode(regs)) {
  48. preempt_disable();
  49. if (kprobe_running() && kprobe_fault_handler(regs, 11))
  50. ret = 1;
  51. preempt_enable();
  52. }
  53. return ret;
  54. }
  55. #else
  56. static inline int notify_page_fault(struct pt_regs *regs)
  57. {
  58. return 0;
  59. }
  60. #endif
  61. /*
  62. * Check whether the instruction at regs->nip is a store using
  63. * an update addressing form which will update r1.
  64. */
  65. static int store_updates_sp(struct pt_regs *regs)
  66. {
  67. unsigned int inst;
  68. if (get_user(inst, (unsigned int __user *)regs->nip))
  69. return 0;
  70. /* check for 1 in the rA field */
  71. if (((inst >> 16) & 0x1f) != 1)
  72. return 0;
  73. /* check major opcode */
  74. switch (inst >> 26) {
  75. case 37: /* stwu */
  76. case 39: /* stbu */
  77. case 45: /* sthu */
  78. case 53: /* stfsu */
  79. case 55: /* stfdu */
  80. return 1;
  81. case 62: /* std or stdu */
  82. return (inst & 3) == 1;
  83. case 31:
  84. /* check minor opcode */
  85. switch ((inst >> 1) & 0x3ff) {
  86. case 181: /* stdux */
  87. case 183: /* stwux */
  88. case 247: /* stbux */
  89. case 439: /* sthux */
  90. case 695: /* stfsux */
  91. case 759: /* stfdux */
  92. return 1;
  93. }
  94. }
  95. return 0;
  96. }
  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, ret;
  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_page_fault(regs))
  135. return 0;
  136. if (unlikely(debugger_fault_handler(regs)))
  137. return 0;
  138. /* On a kernel SLB miss we can only check for a valid exception entry */
  139. if (!user_mode(regs) && (address >= TASK_SIZE))
  140. return SIGSEGV;
  141. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  142. if (error_code & DSISR_DABRMATCH) {
  143. /* DABR match */
  144. do_dabr(regs, address, error_code);
  145. return 0;
  146. }
  147. #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
  148. if (in_atomic() || mm == NULL) {
  149. if (!user_mode(regs))
  150. return SIGSEGV;
  151. /* in_atomic() in user mode is really bad,
  152. as is current->mm == NULL. */
  153. printk(KERN_EMERG "Page fault in user mode with "
  154. "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  155. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  156. regs->nip, regs->msr);
  157. die("Weird page fault", regs, SIGSEGV);
  158. }
  159. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  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. Unfortunately, in the case of an
  163. * erroneous fault occurring 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 possibility 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. /* 8xx sometimes need to load a invalid/non-present TLBs.
  229. * These must be invalidated separately as linux mm don't.
  230. */
  231. if (error_code & 0x40000000) /* no translation? */
  232. _tlbil_va(address, 0, 0, 0);
  233. /* The MPC8xx seems to always set 0x80000000, which is
  234. * "undefined". Of those that can be set, this is the only
  235. * one which seems bad.
  236. */
  237. if (error_code & 0x10000000)
  238. /* Guarded storage error. */
  239. goto bad_area;
  240. #endif /* CONFIG_8xx */
  241. if (is_exec) {
  242. #ifdef CONFIG_PPC_STD_MMU
  243. /* Protection fault on exec go straight to failure on
  244. * Hash based MMUs as they either don't support per-page
  245. * execute permission, or if they do, it's handled already
  246. * at the hash level. This test would probably have to
  247. * be removed if we change the way this works to make hash
  248. * processors use the same I/D cache coherency mechanism
  249. * as embedded.
  250. */
  251. if (error_code & DSISR_PROTFAULT)
  252. goto bad_area;
  253. #endif /* CONFIG_PPC_STD_MMU */
  254. /*
  255. * Allow execution from readable areas if the MMU does not
  256. * provide separate controls over reading and executing.
  257. *
  258. * Note: That code used to not be enabled for 4xx/BookE.
  259. * It is now as I/D cache coherency for these is done at
  260. * set_pte_at() time and I see no reason why the test
  261. * below wouldn't be valid on those processors. This -may-
  262. * break programs compiled with a really old ABI though.
  263. */
  264. if (!(vma->vm_flags & VM_EXEC) &&
  265. (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
  266. !(vma->vm_flags & (VM_READ | VM_WRITE))))
  267. goto bad_area;
  268. /* a write */
  269. } else if (is_write) {
  270. if (!(vma->vm_flags & VM_WRITE))
  271. goto bad_area;
  272. /* a read */
  273. } else {
  274. /* protection fault */
  275. if (error_code & 0x08000000)
  276. goto bad_area;
  277. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  278. goto bad_area;
  279. }
  280. /*
  281. * If for any reason at all we couldn't handle the fault,
  282. * make sure we exit gracefully rather than endlessly redo
  283. * the fault.
  284. */
  285. survive:
  286. ret = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
  287. if (unlikely(ret & VM_FAULT_ERROR)) {
  288. if (ret & VM_FAULT_OOM)
  289. goto out_of_memory;
  290. else if (ret & VM_FAULT_SIGBUS)
  291. goto do_sigbus;
  292. BUG();
  293. }
  294. if (ret & VM_FAULT_MAJOR) {
  295. current->maj_flt++;
  296. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  297. regs, address);
  298. #ifdef CONFIG_PPC_SMLPAR
  299. if (firmware_has_feature(FW_FEATURE_CMO)) {
  300. preempt_disable();
  301. get_lppaca()->page_ins += (1 << PAGE_FACTOR);
  302. preempt_enable();
  303. }
  304. #endif
  305. } else {
  306. current->min_flt++;
  307. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  308. regs, address);
  309. }
  310. up_read(&mm->mmap_sem);
  311. return 0;
  312. bad_area:
  313. up_read(&mm->mmap_sem);
  314. bad_area_nosemaphore:
  315. /* User mode accesses cause a SIGSEGV */
  316. if (user_mode(regs)) {
  317. _exception(SIGSEGV, regs, code, address);
  318. return 0;
  319. }
  320. if (is_exec && (error_code & DSISR_PROTFAULT)
  321. && printk_ratelimit())
  322. printk(KERN_CRIT "kernel tried to execute NX-protected"
  323. " page (%lx) - exploit attempt? (uid: %d)\n",
  324. address, current_uid());
  325. return SIGSEGV;
  326. /*
  327. * We ran out of memory, or some other thing happened to us that made
  328. * us unable to handle the page fault gracefully.
  329. */
  330. out_of_memory:
  331. up_read(&mm->mmap_sem);
  332. if (is_global_init(current)) {
  333. yield();
  334. down_read(&mm->mmap_sem);
  335. goto survive;
  336. }
  337. printk("VM: killing process %s\n", current->comm);
  338. if (user_mode(regs))
  339. do_group_exit(SIGKILL);
  340. return SIGKILL;
  341. do_sigbus:
  342. up_read(&mm->mmap_sem);
  343. if (user_mode(regs)) {
  344. info.si_signo = SIGBUS;
  345. info.si_errno = 0;
  346. info.si_code = BUS_ADRERR;
  347. info.si_addr = (void __user *)address;
  348. force_sig_info(SIGBUS, &info, current);
  349. return 0;
  350. }
  351. return SIGBUS;
  352. }
  353. /*
  354. * bad_page_fault is called when we have a bad access from the kernel.
  355. * It is called from the DSI and ISI handlers in head.S and from some
  356. * of the procedures in traps.c.
  357. */
  358. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  359. {
  360. const struct exception_table_entry *entry;
  361. /* Are we prepared to handle this fault? */
  362. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  363. regs->nip = entry->fixup;
  364. return;
  365. }
  366. /* kernel has accessed a bad area */
  367. switch (regs->trap) {
  368. case 0x300:
  369. case 0x380:
  370. printk(KERN_ALERT "Unable to handle kernel paging request for "
  371. "data at address 0x%08lx\n", regs->dar);
  372. break;
  373. case 0x400:
  374. case 0x480:
  375. printk(KERN_ALERT "Unable to handle kernel paging request for "
  376. "instruction fetch\n");
  377. break;
  378. default:
  379. printk(KERN_ALERT "Unable to handle kernel paging request for "
  380. "unknown fault\n");
  381. break;
  382. }
  383. printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
  384. regs->nip);
  385. die("Kernel access of bad area", regs, sig);
  386. }