fault.c 15 KB

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